About static variable initialization

Here is an exercise where you have to guess the output :-)

publicclass MyClass{

privatestaticint x = getValue();

privatestaticint y = 5;

privatestaticint getValue(){

System.out.print("Running getValue ");

return y;

}

publicstaticvoid main(String[] args){

System.out.println(x);

}

}

This code outputs "Running getValue 0" I don't understand why?

[1109 byte] By [blutch009a] at [2007-11-27 6:20:23]
# 1
because class initialisation will call getValue() before it initialises 7 to 5, thus setting x to the value y has before being initialised which is 0.What this tells you is that you should never program rubbish like that, and in general not rely on the value of uninitialised
jwentinga at 2007-7-12 17:35:32 > top of Java-index,Java Essentials,Training...
# 2
Thanks it was not obvious
blutch009a at 2007-7-12 17:35:32 > top of Java-index,Java Essentials,Training...