Full understanding of the execution order of static code blocks / initial blocks / constructor methods in Java

Posted May 25, 20203 min read

Full understanding of the execution order of static code blocks/initial blocks/constructor methods in Java

" Believe that, after brushing the interview questions, you should have encountered many questions. About the order of execution of static code blocks/initial blocks/constructor methods in Java after class inheritance, I forgot every time I remembered, then, come today Use less time to review "

  1. Execution order of static code block/initial block/constructor

package erdan.demo;
public class Demo {
    static {
        System.out.println("Static Code Block 111");
    }
    static {
        System.out.println("Static code block 222");
    }
    {
        System.out.println("Initial Block 111");
    }
    {
        System.out.println("Initial block 222");
    }
    public Demo() {
        System.out.println("No parameter construction method");
    }
    public Demo(int i) {
        System.out.println("parameter constructor" + i);
    }
    public static void main(String []args) {
        new Demo();
    }
}

Output:

Static code block 111
Static code block 222
Initial block 111
Initial block 222
No parameter construction method

Above:

Note:The initial block(non-static code block) is always the same as the construction method and will appear together

  1. Execution order after test inheritance

package erdan.demo;
public class ChildrenDemo extends Demo {
    static {
        System.out.println("ChildrenDemo:static code block 111");
    }
    static {
        System.out.println("ChildrenDemo:Static code block 222");
    }
    {
        System.out.println("ChildrenDemo Initial Block 111");
    }
    {
        System.out.println("ChildrenDemo initial block 222");
    }
    public ChildrenDemo() {
        System.out.println("No parameter construction method ChildrenDemo");
    }
    public ChildrenDemo(int i) {
        System.out.println("Parametric constructor ChildrenDemo:" + i);
    }
    public static void main(String []args) {
        new ChildrenDemo();
        System.out.println("---------------------------------");
        new ChildrenDemo(1);
    }
}

Output:

Static code block 111
Static code block 222
ChildrenDemo:static code block 111
ChildrenDemo:Static code block 222
Initial block 111
Initial block 222
No parameter construction method
ChildrenDemo Initial Block 111
ChildrenDemo Initial Block 222
No parameter construction method ChildrenDemo
---------------------------------
Initial block 111
Initial block 222
No parameter construction method
ChildrenDemo Initial Block 111
ChildrenDemo Initial Block 222
Parametric construction method ChildrenDemo:1

You can see that in addition to the difference between calling and without parameters, there are fewer static code blocks below the dotted line

ps:

  • Analog subclasses are executed first
  • The state code block will only be output once, the initial block and the construction method are one family, the output is in one block, the initial block will be initialized before construction
  1. Summary

  1. The parent class is executed before the child class
  2. Static code block, when the class is first loaded, it will be initialized once, suitable for initializing global parameters and constants in the project
  3. The initial block and the construction method are a family, but the initial block will be executed before the constructor. The initial block is suitable for overloading the constructor. The same code exists, which can be extracted and used

Public Account