Skip to content
Related Articles

Related Articles

Java | Constructors | Question 2
  • Difficulty Level : Basic
  • Last Updated : 28 Nov, 2013

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

My Personal Notes arrow_drop_up
Recommended Articles
Page :