Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

Output of Java Program | Set 3

  • Difficulty Level : Medium
  • Last Updated : 19 Oct, 2021

Predict the output of the following Java Programs:

Example1:

Attention reader! Don’t stop learning now. Get hold of all the important Java Foundation and Collections concepts with the Fundamentals of Java and Java Collections Course at a student-friendly price and become industry ready. To complete your preparation from learning a language to DS Algo and many more,  please refer Complete Interview Preparation Course.

Java




// filename: Test.java
  
class Test {
  
    // Declaring and initializing integer variable
    int x = 10;
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating an object of class inside main()
        Test t = new Test();
  
        // Printing the value inside the object by
        // above created object
        System.out.println(t.x);
    }
}
Output
10

Output explanation:



In Java, members can be initialized with the declaration of the 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). 

Example  2:

Java




// filename: Test.java
  
// Main class
class Test {
  
    // Declaring and initializing variables
    int y = 2;
    int x = y + 2;
  
    // main driver method
    public static void main(String[] args)
    {
  
        // Creating an object of class inside main() method
        Test m = new Test();
  
        // Printing the value of x and y
        // using above object created
        System.out.println("x = " + m.x + ", y = " + m.y);
    }
}
Output
x = 4, y = 2

Output explanation:

A pretty straightforward solution: As y is initialized first with the value 2, then x is initialized as y + 2. So the value of x becomes 4.

Geek have you ever wondered what will happen when a member is initialized in class declaration and constructor both? 

Example 3:

Java




// filename: Test.java
  
// Main clas
public class Test {
    // Declaring and initializing integer with custom value
    int x = 2;
  
    // Constructor of this class
    // Parametrized constructor
    Test(int i) { x = i; }
  
    // Main driver method
    public static void main(String[] args)
    {
  
        // Creating object of class in main()
        Test t = new Test(5);
  
        // Printing the value
        System.out.println("x = " + t.x);
    }
}
Output



x = 5

Output explanation: 

The initialization with the 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. 

Example 4:

Java




// filename: Test2.java
  
// Class 1
// Helper class
class Test1 {
  
    // Constructor of this class
    Test1(int x)
    {
  
        // Print statement whenever this constructor is
        // called
        System.out.println("Constructor called " + x);
    }
}
  
// Class 2
// Class contains an instance of Test1
// Main class
class Test2 {
  
    // Creating instance(object) of class1 in this class
    Test1 t1 = new Test1(10);
  
    // Constructor of this class
    Test2(int i) { t1 = new Test1(i); }
  
    // Main driver method
    public static void main(String[] args)
    {
        // Creating instance of this class inside main()
        Test2 t2 = new Test2(5);
    }
}
Output
Constructor called 10
Constructor called 5

Output explanation:

First t2 object is instantiated in the main method. As the order of initialization of local variables comes first then 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, the 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
Recommended Articles
Page :

Start Your Coding Journey Now!