In what order do static/instance initializer blocks in Java run?

前端 未结 8 1573
心在旅途
心在旅途 2020-11-22 17:01

Say a project contains several classes, each of which has a static initializer block. In what order do those blocks run? I know that within a class, such blocks are run in

相关标签:
8条回答
  • 2020-11-22 17:41

    You can have multiple static and instance initializers in the same class, therefore

    • Static initializers are called in the textual order they are declared (from 12.4.2)
    • Instance initializers are called in the textual order they are declared (from 12.5)

    Each is executed as if it was a single block.

    0 讨论(0)
  • 2020-11-22 17:43

    There is one case in which a static block will not be called.

    class Super {
        public static int i=10;
    }
    class Sub extends Super {
        static {
            system.out.println("Static block called");
        }
    }
    class Test {
        public static void main (String [] args) {
            system.out.println(Sub.i);
        } 
    }
    

    The above code outputs 10


    Update from an "editor"

    The technical explanation for this is in JLS 12.4.1

    "A reference to a static field (§8.3.1.1) causes initialization of only the class or interface that actually declares it, even though it might be referred to through the name of a subclass, a subinterface, or a class that implements an interface."

    The intuitive explanation is Super.i and Sub.i are actually the same variable, and nothing in Sub actually needs to be initialized for the Super.i to get the correct value.

    (It would be different if the initialization expression for Super.i referred to the Sub class. But then you would have a cycle in the initialization order. A careful reading of JLS 12.4.1 and JLS 12.4.2 explains that this is allowed, and allows you to work out exactly what would happen in practice.)

    0 讨论(0)
  • 2020-11-22 17:44

    Keith's and Chris's answers are both great, I'm just adding some more detail for my specific question.

    Static init blocks run in the order in which their classes are initialized. So, what order is that? Per JLS 12.4.1:

    A class or interface type T will be initialized immediately before the first occurrence of any one of the following:

    • T is a class and an instance of T is created.
    • T is a class and a static method declared by T is invoked.
    • A static field declared by T is assigned.
    • A static field declared by T is used and the field is not a constant variable (§4.12.4).
    • T is a top-level class, and an assert statement (§14.10) lexically nested within T is executed.

    Invocation of certain reflective methods in class Class and in package java.lang.reflect also causes class or interface initialization. A class or interface will not be initialized under any other circumstance.

    To illustrate, here's a walkthrough of what's happening in the example:

    1. Enter main
    2. Print "START"
    3. Attempt to create first instance of Child, which requires initialization of Child
    4. Attempting to initialize Child causes initialization of Parent
    5. Attempting to initialize Parent causes initialization of Grandparent
    6. At the start of initialization of Grandparent, Grandparent's static initialization block is run
    7. Technically, Object gets the last say in the initialization chain by virtue of being Grandparent's parent, but it has nothing to contribute
    8. After Grandparent's static initialization block ends, program falls back to Parent's static initialization block
    9. After Parent's static initialization block ends, program falls back to Child's static initialization block
    10. At this point, Child is initialized, so its constructor may proceed
    11. Since IAmAClassThatIsNeverUsed never gets referenced, none of its code ever runs, including static initializer blocks
    12. The rest of this walkthrough doesn't concern static initializers and is included only for completeness
    13. Child's constructor implicitly calls super() (i.e., Parent's constructor)
    14. Parent's constructor implicitly calls super() (i.e., Grandparent's constructor)
    15. Grandparent's constructor does the same, which has no effect (again, Object has nothing to contribute)
    16. Immediately after Grandparent's constructor's call to super() comes Grandparent's instance initializer block
    17. The rest of Grandparent's constructor's constructor runs and the constructor terminates
    18. The program falls back to Parent's constructor, immediately after its call to super() (i.e., Grandparent's constructor) resolves
    19. As above, Parent's instance initializer does its thing and its constructor finishes up
    20. Similarly, the program returns to and completes Child's constructor
    21. At this point, the object has been instantiated
    22. Print "END"
    23. Terminate normally
    0 讨论(0)
  • 2020-11-22 17:46

    The static initializer for a class gets run when the class is first accessed, either to create an instance, or to access a static method or field.

    So, for multiple classes, this totally depends on the code that's run to cause those classes to get loaded.

    0 讨论(0)
  • 2020-11-22 17:46
    class A {
      public A() { 
        // 2
      }
    }
    
    class B extends A{
      static char x = 'x'; // 0
      char y = 'y'; // 3
      public B() { 
        // 4
      }
    
      public static void main(String[] args) {
        new B(); // 1
      }
    }
    
    

    Numbers in the comment indicate the evaluation order, the smaller, the earlier.

    As the example showed,

    1. static variable
    2. main
    3. constructor of superclass
    4. instance variable
    5. constructor
    0 讨论(0)
  • 2020-11-22 17:48

    See section 12.4 and 12.5 of the JLS version 8, they go into gory detail about all of this (12.4 for static and 12.5 for instance variables).

    For static initialization (section 12.4):

    A class or interface type T will be initialized immediately before the first occurrence of any one of the following:

    • T is a class and an instance of T is created.
    • T is a class and a static method declared by T is invoked.
    • A static field declared by T is assigned.
    • A static field declared by T is used and the field is not a constant variable (§4.12.4).
    • T is a top level class (§7.6), and an assert statement (§14.10) lexically nested within T (§8.1.3) is executed.

    (and several weasel-word clauses)

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