Possible Duplicate:
Java static class initialization
in what order are static blocks and static variables in a class executed?
When I run this code the answer is 1, I thought it would be 2. What is the order of initialization and the value of k in each step?
public class Test {
static {k = 2;}
static int k = 1;
public static void main(String[] args) {
System.out.println(k);
}
}
Edit 1: As a follow up to "k is set to default value" then why this next code doesn't compile? Theres an error "Cannot reference a field before it's defined".
public class Test {
static {System.out.println(k);}
static int k=1;
public static void main(String[] args) {
System.out.println(k);
}
}
Edit 2: For some unknow to me reason it^ works when instead of "k" its "Test.k".
Thanks for all the answers. this will sufice :D
They are executed in the order that you write them. If the code is:
then the output becomes 2.
The order of initialization is: ..the class variable initializers and static initializers of the class..., in textual order, as though they were a single block.
And the values (for your code) are: k = 0 (default), then it's set to 2, then it's set back to 1.
You can check that it's actually set to 2 by running the following code:
Short answer
When the initialization of the class starts,
k
will have initial value of 0.The static block (since it precedes the assignment in the declaration) is then executed, and
k
will be assigned 2.Then the initializer in the declaration is executed, and
k
will be assigned 1.Long explanation
Let us use this example, since your example is a bit simple:
According to Java Language Specification, from section 4.12.5:
(The following lines from the specification specify the default value for all the types, basically some form of 0, such as
0
,0.0d
,null
,false
, etc.)So before the class is initialized (due to one of these reasons), the variables will hold an initial value.
According to the detailed initialization procedure (only the interesting steps are quoted here, and emphasis mine):
Let us look at step 6, with the 4
final
class variables:stat1
,str2
,second
,lazy
.Since
10
is constant expression, and so is"sdfff"
, and due to the order of execution, it is not possible to observe the initial value forstr2
andstat1
. In order to make an observation, the earliest you can do is in step 9.The case of
second
demonstrate that when the right hand side is not a compile-time constant expression, so its initial value is visible.The case of
lazy
is different, since the assignment is done in static block, and hence happen in step 9 - so it is possible to observe its initial value. (Well, the compiler checks carefully thatlazy
is assigned exactly once).After the initialization of final class variables with compile-time constant expression comes the execution of static blocks and the rest of the initializers.
As you can see from the example, the static blocks and initialization happens according to textual order - demonstrated with the use of
str
variable - it is first printed out asnull
, thensomething
, thencrap
.