Output of Java Program | Set 3

Predict the output of following Java Programs:

filter_none

edit
close

play_arrow

link
brightness_4
code

// filename: Test.java
class Test {
    int x  = 10;  
    public static void main(String[] args) {    
         Test t = new Test();
         System.out.println(t.x);
    }
}

chevron_right


The program works fine and prints 10. Unlike C++, in Java, members can initialized with declaration of class. This initialization works well when the initialization value is available and the initialization can be put on one line (See this for more details). For example, the following program also works fine.

filter_none

edit
close

play_arrow

link
brightness_4
code

// filename: 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);
    }
}

chevron_right


Output of the above program is “x = 4, y = 2”. y is initialized first, then x is initialized as y + 2. So the value of x becomes 4.

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

filter_none

edit
close

play_arrow

link
brightness_4
code

// filename: 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);
    }
}

chevron_right


Output of the above program is “x = 5”. The initialization with class declaration in Java is like initialization using Initializer List in C++. So, in the above program, the value assigned inside the constructor overwrites the previous value of x which is 2, and x becomes 5.



As an exercise, predict the output of following program.

filter_none

edit
close

play_arrow

link
brightness_4
code

// 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);
    }
}

chevron_right


The output of the program is Constructor called 10 Constructor called 5.
First t2 object is instantiated in the main method. As the order of initialization of local variables comes first than the constructor,first the instance variable (t1), in the class Test2 is allocated to the memory. In this line a new Test1 object is created, constructor is called in class Test1 and ‘Constructor called 10’ is printed. Next the constructor of Test2 is called and again a new object of the class Test1 is created and ‘Constructor called 5’ is printed.

Please write comments if you find any of the answers/explanations incorrect, or want to share more information about the topics discussed above.



My Personal Notes arrow_drop_up

Improved By : RupaShankar



Article Tags :
Practice Tags :


1


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.