Initialization order of Java classes (execution order of static code block constructors, etc.)

The initialization sequence of Java classes may include the following scenarios:

Java parent constructor, static member variable of parent class, common member variable of parent class, static code block of parent class, code block of parent class,
Subclass constructor subclass static member variable, subclass common member variable, subclass static code block, subclass code block execution order

Without inheritance

1. Execution sequence 
    1. Static member variable
    2. Static code block
    3. Common member variable
    4. Common code block
    5. Constructor

2. Conclusion:

        1. static state->ordinary
        2. variable->Code block->Constructor
        3. Constructor is last executed

3. Simple code implementation:

    public class Father {

        private static String name = "zct";
        private int age = 50;

        {
            System.out.println("father age: "+age);
            System.out.println("father I'm a normal code block");
        }

        static{
            System.out.println("father static name: "+name);
            System.out.println("father I am a static code block");
        }

        public Father(){
            System.out.println("father I'm a constructor");
        }

    }

4. Test:

    @org.junit.Test
    public void test(){
      Father father = new Father();
    }

5. Test results:

    father static name: zct
    father I am a static code block
    father age: 50
    father I'm a normal code block
    father I'm a constructor

When a subclass inherits a parent

1. Execution sequence 
    1. Static member variable of parent class
    2. Static code block of parent class
    3. Static member variable of subclass
    4. Static code block of subclass
    5. Member variable of parent class
    6. Code block of parent class
    7. Constructor of the parent class
    8. Member variable of subclass
    9. Code block of subclass
    10. Constructor of subclass

2. Conclusion:

    1. First parent class then child class
    2. If the subclass has static member variables and static code blocks, after the static member variables and static code blocks of the parent class are executed, the static variables and static code blocks of the subclass are executed,
    //Otherwise, directly follow the parent variable - > code block - > constructor, and then execute the child variable - > code block - > constructor
    3. It should be noted that the static variables and code blocks of subclasses take precedence over the common member variables and code blocks and constructors of the parent class.
    4. This also shows that static first->More common

3. Simple code implementation:

    public class Son extends Father{

        private static String name = "zlc";
        private int age = 26;
        {
            System.out.println("son age: "+age);
            System.out.println("son I'm a normal code block");
        }

        static{
            System.out.println("son static name: "+name);
            System.out.println("son I am a static code block");
        }

        public Son(){
            System.out.println("son I'm a constructor");
        }
    }   

4. Test:

    @org.junit.Test
    public void test(){
        Son son = new Son();
    }

5. Test results:

    father static name: zct
    father I am a static code block
    son static name: zlc
    son I am a static code block
    father age: 50
    father I'm a normal code block
    father I'm a constructor
    son age: 26
    son I'm a normal code block
    son I'm a constructor

 

Tags: Java Junit

Posted on Sun, 10 Nov 2019 13:35:36 -0800 by priya_cks