Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
878 views
in Technique[技术] by (71.8m points)

jvm - explain java compile order

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'torv1 [%s]v2 [%s]
", 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'torv1 [%s] v2 [%s] v3 [%s] v4 [%s]
", 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
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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 before v2, which is why v2 is still null.
  • 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:

  1. 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.

...

  1. 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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.7k users

...