Open In App

Java | Constructors | Question 2

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

Article Tags :