Static initializer runs after the constructor, why

2019-01-22 19:19发布

问题:

I have 2 classes:

Class A:

public class A {
    static B b = new B();

     static {
         System.out.println("A static block");
     }

     public A() {
         System.out.println("A constructor");
     }
}

Class B:

public class B {
     static {
         System.out.println("B static block");
         new A();
     }

     public B() {
         System.out.println("B constructor");
     }
}

I create a Main class which just creates new A:

public class Main {
    public static void main(String[] args) {
        new A();
    }
}

The output I get is:

B static block
A constructor
B constructor
A static block
A constructor

As you can see, the constructor of A is invoked before its static initializer.

I understand it got something to do with the cyclic dependency I created but I was under the impression the static initializer should always run before the constructor.

What is the reason for this to happen (technically in the java implementation) ?

Is it recommended to avoid static initializers all together ?

回答1:

static B b = new B();

is before

static {
     System.out.println("A static block");
}

So you require that the B instance be initialized before you print "A static block".

And initializing the B class means you need to create a A instance. So there's no way for "A static block" to be printed before the A instance is constructed.

Yes, the static initialization of A is launched before the constructor is launched but, apart deadlocking, there would be no other solution to the sequence you require.

Note the warning in the specification :

Because the Java programming language is multithreaded, initialization of a class or interface requires careful synchronization, since some other thread may be trying to initialize the same class or interface at the same time. There is also the possibility that initialization of a class or interface may be requested recursively as part of the initialization of that class or interface; for example, a variable initializer in class A might invoke a method of an unrelated class B, which might in turn invoke a method of class A. The implementation of the Java virtual machine is responsible for taking care of synchronization and recursive initialization by using the following procedure [the doc goes on with the complete procedure]

A best practice, in Java as in other languages, is basically to avoid cyclic dependencies as their resolution may be very hard to predict.