问题
v2
is null when it enters A's c'tor at the first time, but if I put v2
's declaration & initialization before instance
it will have a value; why is this?
public class A {
private static final String v1 = new String(new byte[]{'a', 'b'});
private static A instance = new A();
private static final String v2 = new String(new byte[]{'b', 'c'});
private A() {
System.out.printf("A c'tor\tv1 [%s]\tv2 [%s]\n", v1, v2);
}
public static void main(String[] args) {
System.out.println("start main");
new A();
System.out.println("end main");
}
}
output:
A c'tor v1 [ab] v2 [null]
start main
A c'tor v1 [ab] v2 [bc]
end main
Also, if I change v2
's initialization to instead be:
private static final String v2 = "ab";
it does initialize v2
, and the output is:
A c'tor v1 [ab] v2 [ab]
start main
A c'tor v1 [ab] v2 [ab]
end main
Edit
another test for the second part:
public class A {
private static final String v1 = new String(new byte[]{'a', 'b'});
private static transient A instance = new A();
private static final String v2 = new String(new byte[]{'b', 'c'});
private static final String v3 = new String("ab");
private static final String v4 = "ab";
private A() {
System.out.printf("A c'tor\tv1 [%s] v2 [%s] v3 [%s] v4 [%s]\n", v1, v2, v3, v4);
}
public static void main(String[] args) {
System.out.println("start main");
new A();
System.out.println("end main");
}
}
output:
A c'tor v1 [ab] v2 [null] v3 [null] v4 [ab]
start main
A c'tor v1 [ab] v2 [bc] v3 [ab] v4 [ab]
end main
回答1:
The static variables are initialized in the order they appear in the source file when class A is initialized (before main
is executed).
- first v1
- then instance (which prints
A c'tor v1 [ab] v2 [null]
since its initialization involves creating an instance of A) - it is initialized beforev2
, which is whyv2
is stillnull
. - then v2
After they are initialized, the main
method is executed, and produces the next 3 output lines. The creation of the new instance of A
inside the main
method produces a different output than the previous constructor call, since at this point both v1
and v2
are initialized.
EDIT :
Regarding your updated question :
If you follow the initialization procedure in JLS 12.4.2:
- Otherwise, record the fact that initialization of the Class object for C is in progress by the current thread, and release LC. Then, initialize the final class variables and fields of interfaces whose values are compile-time constant expressions.
...
- Next, execute either the class variable initializers and static initializers of the class, or the field initializers of the interface, in textual order, as though they were a single block.
As you can see, the final static variables whose values are compile-time constant expressions are initialized before the rest of the static variables. Therefore changing the value of v2
to the constant "ab"
causes v2
to be initialized before the instance
variable, which explains the different output.
来源:https://stackoverflow.com/questions/37439450/explain-java-compile-order