Open In App

Java | Constructors | Question 2

Like Article
Like
Save
Share
Report

Predict the output of following Java program




class T {
  int t = 20;
  T() {
    t = 40;
  }
}
class Main {
   public static void main(String args[]) {
      T t1 = new T();
      System.out.println(t1.t);
   }
}


(A) 20
(B) 40
(C) Compiler Error


Answer: (B)

Explanation: The values assigned inside constructor overwrite the values initialized with declaration.

Quiz of this Question


Last Updated : 28 Jun, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads