Static initializer runs after the constructor, why?

后端 未结 1 941
终归单人心
终归单人心 2020-12-14 16:25

I have 2 classes:

Class A:

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

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

     pu         


        
相关标签:
1条回答
  • 2020-12-14 17:03
    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.

    0 讨论(0)
提交回复
热议问题