In what order do static blocks and initialization

2019-01-04 06:48发布

I have two classes Parent and Child

public class Parent {    
    public Parent() {
        System.out.println("Parent Constructor");
    }    
    static {
        System.out.println("Parent static block");    
    }    
    {
        System.out.println("Parent initialisation  block");
    }
}

public class Child extends Parent {    
    {
        System.out.println("Child initialisation block");
    }
    static {
        System.out.println("Child static block");
    }

    public Child() {
        System.out.println("Child Constructor");
    }    
    public static void main(String[] args) {
        new Child();    
    }
}

The output of the above code will be

Parent static block
Child static block
Parent initialization  block
Parent Constructor
Child initialization block
Child Constructor

Why does Java execute the code in that order? What are the rules that determine the execution order?

11条回答
【Aperson】
2楼-- · 2019-01-04 07:08
  • Static init blocks are executed at the time of class loading.
  • In the class hierarchy the order for execution of static init blocks will start from top level class.
  • In a class the order for the execution of static block is from top to bottom.
  • Above rule apply regardless of where the static block is present within the class.

(In your code the parent static blocks will be executed first and then the child class static blocks.)

  • Instance init blocks will be executed after the call to the super(); in the constructor.
    • Always super(); is the very first statement in a default constructor.

In your code when you create a Child object:

  • The default constructor of the Child class get executed.
  • It will call to the super(); constructor.
  • Then the super class constructor is executed.
  • The Parent class will execute its super(); call.
  • After that the instance init blocks in the Parent class are executed.(From top to bottom).
  • Then the code within the constructor is executed (if any).
  • Then it will return to the Child class and execute the Child class instance init blocks.
  • Finally the code in the child constructor get executed (If exists).
查看更多
爷的心禁止访问
3楼-- · 2019-01-04 07:15

There are several rules in play

  • static blocks are always run before the object is created, so that's why you see print messages from both parents and child static blocks
  • now, when you are calling constructor of the subclass (child), then this constructor implicitly calls super(); before executing it's own constructor. Initialization block comes into play even before the constructor call, so that's why it is called first. So now your parent is created and the program can continue creating child class which will undergo the same process.

Explanations:

  1. Static block of parent is executed first because it is loaded first and static blocks are called when the class is loaded.
查看更多
放我归山
4楼-- · 2019-01-04 07:18

Here is what I found while preparing for a certification.

While we run a class, first static blocks/ static variable initialisation happens. If multiple static blocks are there, it will execute it in the order in which it appears,

Then it will execute init blocks/ instance variable initialisation.If multiple init blocks/ variable initialisation are there, it will execute it in the order in which it appears,

Afterwards it will look into the constructor.

查看更多
可以哭但决不认输i
5楼-- · 2019-01-04 07:21

Static block gets executed when a class is loaded into JVM. While init block gets copied into the Constructor whose object will be created and runs before creation of object.

查看更多
该账号已被封号
6楼-- · 2019-01-04 07:22

First - run child class only (comment the extend clause) to see the simple flow.

second - go to Static block vs. initializer block in Java? & read the accepted answer over there.

Edit:

  1. Execution happens in SIC way - Static, (non static) Initializer & Constructor.
  2. (Non static) Initializer are copied into every constructor - At the TOP! (hence lines 3/4/5/6)
  3. Before a class is initialized, its direct superclass must be initialized - http://docs.oracle.com/javase/specs/jls/se7/html/jls-12.html#jls-12.4 (hence parent static block appears first).
查看更多
你好瞎i
7楼-- · 2019-01-04 07:24

control flow is-

static block -> Initialization block -> and finally Constructor.

static block -> This static block will be get executed only once when the control come to the class.(JVM Load this class)

Initialization block -> This Initialization block will be get executed whenever a new object Created for the Class (It will be executed from second statement of the Constructor then following constructor statements- remember First statement of the Constructor will be Super()/this())

Constructor -> This will be get whenever a new object is created.

查看更多
登录 后发表回答