Given class A { int x = 1; { x = 2; } A() { x = 3; } }, what does System.out.println(new A().x); print?
Correct answer C. 3
Explanation
When an object is created, field initialisers and instance initialiser blocks run in textual order after the superclass constructor returns, and the rest of the constructor body runs last. So x becomes 1, then 2, and finally 3. The constructor body always has the last word, which is why 2 is a tempting but wrong choice.
Answer checked How we write and check questions
Discussion