public class AQuestion { private int i = giveMeJ(); private int j = 10; private int giveMeJ() { return j; } public static void main(String args[]) { System.out.println((new AQuestion()).i); } } Answers 1.Compiler error complaining about access restriction of private variables of AQuestion. 2.Compiler error complaining about forward referencing. 3.No Compilation error - The output is 0; 4.No Compilation error - The output is 10; //answer is 3. //but if the j is static, answer is 4. can anyone explain why??? thanks!
Bharatesh H Kakamari
Ranch Hand
Joined: Nov 09, 2000
Posts: 198
posted
0
The order of loading : 1. static vars / blocks in the order specified in the class 2. Instance vars block in the order specified in the class 3. Constructor Hence the answer is 4 when the var is changed to static
asheesh talwar
Ranch Hand
Joined: Dec 10, 2000
Posts: 31
posted
0
Bharatesh I am not getting why for the first time the o/p is 0 & not 10 & when we change it to static why are we getting 10 ? Can U please explain the logic behind this. Regards Asheesh