public class Myclass { private static int x = getValue(); private static int y = 5; private static int getValue() { return y; } public static void main(String[] args) { System.out.println(x); } }
Step 1. private static int x = getValue(); This makes a call to the getValue(). Step 2. The getValue is coded to return the value of y. But at this stage y is yet to be initialized. So the default value of an int i.e. zero is returned. So we have the condition x=0 Step 3. Now y is initialized with 5 and we have y=5;
Because the way the code is written, the impression is that the o/p will be 5. [ August 23, 2007: Message edited by: Maneesh Godbole ]