The order of code execution is very basic, but many people don’t understand the details. I’ve made up a catchphrase about the order in which Java code is executed that you’ll never get stuck with again.

The rhyme goes like this:

  • Subclasses load their parents quickly,
  • Load initialization static,
  • Static member Static block,
  • Object is fast to construct a parent class,
  • Object member public block,
  • Finally, the building block is executed.

This is the order in which the code is executed, and it is quite simple, as explained below.

Java code execution order

Class loading phase

I’m sure you know enough about the class loading phase, but if not, I suggest you read my article on what you don’t know about virtual machine class loading. Then understand the first three sentences of this rhyme.

  • Subclasses load their parents quickly

That is, the parent class must be loaded before a subclass is loaded.

  • Load initialization static

Static members and static code blocks are metadata information for a class that is executed when the class is initialized.

  • Static member code block

After the initialization of the static member, the static code block executes.

Program run phase

Now understand the last three sentences of this rhyme. This is done during the class-loading phase, when the program is running.

  • Object is fast to construct a parent class

When creating a new object, the parent constructor must be the first to invoke the super function. This is because if parent has a default constructor, the subclass doesn’t need to display the call. Otherwise it has to be done.

  • Object member public block

The process of object initialization starts with the initialization of object members, followed by the execution of common code blocks.

A common code block is the common part of all overloaded constructors and is a piece of code like this:

{
    System.out.println("Construct code block" );
}
Copy the code
  • Finally, the building block is executed

Object members and code blocks are executed before the constructor. If there is a parent, it is executed in the order of the parent member, the parent code block, the parent constructor, and then the child.

Concrete example

It is recommended to run the following example to understand this rhyme.

public class Parent {
    static int staticV;
    static {
        System.out.println("staticV = " + staticV);
        System.out.println("Parent static code block" );

    }
    int v;
    {
        System.out.println("v = " + v);
    }
    {
        System.out.println("Parent construct code block" );
    }

    public Parent(int v) {
        this.v = v;
        System.out.println("Parent constructor"+v); }}Copy the code
public class Child extends Parent{


    static int staticV;
    static {
        System.out.println("staticV = " + staticV);
        System.out.println("Child static code block" );

    }
    int v;
    {
        System.out.println("v = " + v);
    }
    {
        System.out.println("Child construct code block" );
    }

    public Child(a) {
        super(1);
        System.out.println("Child constructor" );
    }


    public static void main(String[] args) {
        Child child = newChild(); }}Copy the code

The run returns:

StaticV = 0 parent staticV = 0 child Parent constructor 1 v = 0 child Child constructorCopy the code

Matters needing attention

As mentioned above, if parent does not have a default no-argument constructor, then the parent constructor must be displayed in the subclass, otherwise the program will fail to compile.