Java | Constructors | Question 2

Predict the output of following Java program

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


(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


Article Tags :

1


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