This is because of the order of initialization of static members - blocks, variables etc.
The cache class gets loaded and all of its static code is executed at that time. Static initializers are executed at the same time that static variables are initialized. The initializations occur in lexical order.
So this gives your first output line:
Over here, the first thing is the static block that calls the static method initializeIfNecessary() which has reference to the 2 variables, declared static. So default values are taken here and code is executed. In the second step it goes for next static member -> sum, for which the values has already been calculated.
Then comes the boolean member and its value is rewritten to false.
If you omit out this assignment of false, you will see that value - true set during initializeIfNecessary() is retained.
Hence your other 2 outputs.
Joined: Jul 14, 2008
Thanks for your reply.
But I really want to know what happens when the static block calls any static function with assignments within it &
when main method calls up the static method ......
I actually what to know what happens to assignments??