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); } } |
chevron_right
filter_none
(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
Recommended Posts:
- Java | Constructors | Question 1
- Java | Constructors | Question 3
- Java | Constructors | Question 4
- Java | Constructors | Question 5
- Java | Constructors | Question 6
- Java | Constructors | Question 8
- Why Constructors are not inherited in Java?
- Order of execution of Initialization blocks and Constructors in Java
- C++ | Constructors | Question 2
- C++ | Constructors | Question 3
- C++ | Constructors | Question 4
- C++ | Constructors | Question 5
- C++ | Constructors | Question 6
- C++ | Constructors | Question 7
- C++ | Constructors | Question 8
- C++ | Constructors | Question 9
- C++ | Constructors | Question 10
- C++ | Constructors | Question 17
- C++ | Constructors | Question 12
- C++ | Constructors | Question 13

