raj chiru wrote:
Actually where I'm confusing is,we are explicitly declared the static field values,right?But why First,static fields are set to default values?
Well, which is worse? A default value or an undefined random value?
From this puzzle, you can see that initialization is not atomic, and it is possible to see variables before they get assigned. So, yes. Static fields are set to default values -- because the values is not known yet. But that is better than an undefined random value, which is what it would be seen as if the variable isn't set.
BTW, there is an exception to this rule. If the static variable is a compile time constant, then the value is guarranteed to be known before it is used -- and it will be set correctly to the compile time constant value.
Henry