Difficulty level: medium

Predict the output of the following Java program:

// File name: test.java
class Test {
	int x = 10;
	public static void main(String[] args) {
		Test t = newTest(); System.out.println(t.x); }}Copy the code

The program runs fine and prints 10. In Java, members can be initialized using class declarations. This initialization works fine when the initialization value is available and the initialization can be placed on a single line (see here for more details). For example, the following program also works.

// File name: test.java
class Test {
	int y = 2;
	int x = y+2;
	public static void main(String[] args) {
		Test m = new Test();
		System.out.println("x = " + m.x + ", y = "+ m.y); }}Copy the code

The output of the above program is “x = 4, y = 2”. Y is initialized first, and then x is initialized to y plus 2. So the value of x becomes 4.

What happens when a member is initialized in both the class declaration and the constructor? Consider the following procedure.

// File name: test.java
public class Test
{
	int x = 2;
	Test(int i) { x = i; }
	public static void main(String[] args) {
		Test t = new Test(5);
		System.out.println("x = "+ t.x); }}Copy the code

The output of the above program is “x = 5”. Initialization using class declarations in Java is similar to that using the Initializer List in C++. So, in the above program, the value assigned inside the constructor overrides the value 2 before x, and x becomes 5. As an exercise, predict the output of the following program.

// filename: Test2.java
class Test1 {
	Test1(int x) {
		System.out.println("Constructor called "+ x); }}// This class contains an instance of Test1
class Test2 {
	Test1 t1 = new Test1(10);

	Test2(int i) { t1 = new Test1(i); }

	public static void main(String[] args) {
		Test2 t2 = new Test2(5); }}Copy the code

The output of the program is Constructor called 10 Constructor called 5. First, instantiate the T2 object in the main method. Since local variables are initialized earlier than constructors, the instance variable (T1) is first allocated into memory in class Test2. In this line, you create a new Test1 object, call the Constructor in class Test1 and print “Constructor called 10”. Next, the Constructor for Test2 is called, creating a new object for class Test1 again and printing “Constructor called 5”.


Related articles:

The output of a Java program | Java exercises 】 【 first (resolution)

The output of a Java program | Java exercises 】 【 a second (resolution)

The output of a Java program | Java exercises 】 【 a third (resolution)

The output of a Java program | Java exercises 】 【 4 sets (including parsing)

I’ve been writing a technology blog for a long time, and this is one of my Java worksheets. Hope you like it! More related articles and my contact information I put here:

Github.com/wanghao221 gitee.com/haiyongcsdn…

If you do learn something new from this post, like it, bookmark it and share it with your friends. 🤗 Finally, don’t forget ❤ or 📑 for support