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); } }
Ajay Chandravadiya
Greenhorn
Joined: Aug 06, 2007
Posts: 27
posted
0
stumped to see the output! how is it initializing it?can someone explain to length please? (good question buddy!)
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 ]