Tips

  • Non-static initializers basically work as constructors to avoid code duplication in constructors. The initialization block is executed at each construction of the class
  • A static initialization block is executed only once during class loading

An interesting blind spot:

When PSVM is executed, the constructor of the class is not executed, that is, the instance of the class does not exist.

public static void main(String[] args) {}
Copy the code

Test execution sequence

C extens B extends A new C()

public class InitLearn {

    static class A{
        static Print  pA1 =new Print("static pA1");
        static {
            System.out.println("Static Initialization Block A");
        }
        static Print  pA2 =new Print("static pA2");
        {
            System.out.println("Initialization Block A Before Construct");
        }
        public A(a){
            System.out.println("Construct A");
        }
        {
            System.out.println("Initialization Block A After Construct"); }};static class B extends A{
        static Print  pB1 =new Print("static pB1");
        static Print  pB2 =new Print("static pB2");
        static {
            System.out.println("Static Initialization Block B");
        }
        {
            System.out.println("Initialization Block B Before Construct");
        }
        public B(a){
            System.out.println("Construct B");
        }
        {
            System.out.println("Initialization Block B After Construct"); }};static class C extends B{
        static Print  pC1 =new Print("static pC1");
        static {
            System.out.println("Static Initialization Block C");
        }
        static Print  pC2 =new Print("static pC2");
        {
            System.out.println("Initialization Block C Before Construct");
        }
        public C(a){
            System.out.println("Construct C");
        }
        {
            System.out.println("Initialization Block C After Construct"); }};static class Print{
        public Print(String v){ System.out.println(v); }}static {
        System.out.println("static{} of the class run psvm to test");
    }
    public static void main(String[] args) {
          new C();
          //new C();}}Copy the code

Output:


static{} of the class run psvm to test// Test class static initializer block in callpsvmbeforestatic pA1 //AClass static members are defined before the static initialization blockStatic Initialization Block A //AClass static initializer blockstatic pA2 //AClass static members are defined after a statically initialized blockstatic pB1 //BClass static members are defined before the static initialization blockstatic pB2 //BClass static members are defined before the static initialization blockStatic Initialization Block B //BClass static initializer blockstatic pC1  //CClass static members are defined before the static initialization blockStatic Initialization Block C //CClass static initializer blockstatic pC2  //CClass static members are defined after a statically initialized blockInitialization Block A Before Construct //AClass initialization blockInitialization Block A After Construct  //AClass initialization blockConstruct A  //AClass constructorInitialization Block B Before Construct 
Initialization Block B After Construct
Construct B
Initialization Block C Before Construct
Initialization Block C After Construct
Construct C
    
    
Copy the code

Interesting phenomenon

  • The InitLearn class is definitely not instantiated, but since InitLearn is used to execute the main entry function, the static initialization block is also executed;

  • All static initializer blocks are executed first, followed by non-static initializer blocks and constructors, which are executed in this order:

    1. Static initializer block/static member of the parent class, in declaration order
    2. The static initializer block/static member of a subclass, in declaration order
    3. Initialization blocks for the parent class (whether defined before or after the constructor)
    4. Constructor of the parent class
    5. Initialization blocks for subclasses (whether defined before or after the constructor)
    6. The constructor of a subclass
  • If a new B() is created after that, the following log appears, and the static initialization process is performed only once

    Initialization Block A Before Construct Initialization Block A After Construct Construct A Initialization Block B Before  Construct Initialization Block B After Construct Construct B Initialization Block C Before Construct Initialization Block C After Construct Construct CCopy the code

conclusion

  1. Static initialization blocks have the highest priority, that is, they are executed first and only when the class is first loaded;
  2. Non-statically after initializing blocks and constructors, and once each time an object is generated;
  3. Non-statically initializing block code is executed before the class constructor. To use it, get in the habit of writing initialization blocks before constructors for debugging purposes.
  4. Static initialization blocks can be used either to initialize static member variables or to execute initialization code;
  5. A non-static initialization block can reuse code for multiple overloaded constructors.

reference

www.cnblogs.com/BlackStorm/…