In what order do static blocks and initialization blocks execute when using inheritance?

我的梦境 提交于 2019-11-26 15:42:53
Petr Mensik

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.

I learn visually, so here's a visual representation of order, as a SSCCE:

public class Example {

  static {
    step(1);
  }

  public static int step_1 = step(2);
  public int step_6 = step(6);

  public Example() {
    step(8);
  }

  {
    step(7);
  }

  // Just for demonstration purposes:
  public static int step(int step) {
    System.out.println("Step " + step);
    return step;
  }
}

public class ExampleSubclass extends Example {

  {
    step(9);
  }

  public static int step_3 = step(3);
  public int step_10 = step(10);

  static {
    step(4);
  }

  public ExampleSubclass() {
    step(11);
  }

  public static void main(String[] args) {
    step(5);
    new ExampleSubclass();
    step(12);
  }
}

This prints:

Step 1
Step 2
Step 3
Step 4
Step 5
Step 6
Step 7
Step 8
Step 9
Step 10
Step 11
Step 12

Keep in mind that the order of the static parts matters; look back at the difference between the order of Example's static stuff and ExampleSubclass's.

Also note that the instance initialization block is always executed before the constructor, no matter the order. However, order does matter between an initialization block and a field initializer.

Raúl

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).
Sayanthan Mahendran
  • 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).

Static block in java is executed before main method. If we declare a Static block in java class it is executed when class loads. This is initialize with the static variables. It is mostly used in JDBC. Static block in java is executed every time when a class loads. This is also known as Static initialization block. Static block in java initializes when class load into memory , it means when JVM read the byte code. Initialization can be anything; it can be variable initialization or anything else which should be shared by all objects of that class. Static block is a normal block of code enclosed in braces { } and is preceded by static keyword.

so static block executed first.

Instance Initialization Blocks: Runs every time when the instance of the class is created.

so next Initialization block executed when instance of the class is created.

then Constructor executed

It would be very helpful to ckeck out the object construction process with a step by step debuger, having a view in which you can see how your object is goning through the phases. I found this very useful for clearing the perspective from a higher point of view. Eclipse can help you with this with it's debugger step into function.

Just wanted to share my findings. I read in one of the answers on another thread that static blocks are executed first before static fields which is not correct. It depends on which comes first, static field or static block. Have a look at below code. It will try to put things in perspective.

  1. JVM looks for a class which has public static void main(String args[]) so that it can load that class.
  2. It then initialises static fields of this class(if they come before static blocks). These fields can call static methods of this class or another. If they call static method of this class then that method gets served. If they call static method of another class, then static fields or blocks of that class(depending on which comes first) gets initialised first, then this method call is served.
  3. Then, it moves to static blocks.
  4. It comes back to main method.

    class TestLab {
    static int method(String a) {
        System.out.println("in static method of TestLab" + " Coming from " + a);
        System.out.println("b is " + b);
        return 6;
    }
    
    static int a = method("Line 11");
    static int b = 7;
    
    TestLab() {
        System.out.println("Inside test lab constructor");
    }
    
    static {
        System.out.println("In static block of TestLab");
    }
    
    }
    
    public class Test1 {
    public static void main(String[] args) {
        System.out.println("inside main method of Test 1");
        int a = TestLab.method("Line 26");
    }
    
    // static Test ref=new Test();
    Test1() {
        System.out.println("Default Constructor of Test1");
    }
    
    {
        System.out.println("In instance block of Test1");
    }
    static int d = TestLab.method("Line 37");
    static int e = methodOfTest1();
    static {
        System.out.println("In Static Block of Test1");
    }
    
    static int methodOfTest1() {
        System.out.println("inside static method:mehtodOfTest1()");
        return 3;
    }
    }
    

Here is the output:

in static method of TestLab Coming from Line 11
b is 0
In static block of TestLab
in static method of TestLab Coming from Line 37
b is 7
inside static method:mehtodOfTest1()
In Static Block of Test1
inside main method of Test 1
in static method of TestLab Coming from Line 26
b is 7

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.

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.

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.

1.Static init block executes at the time of class loading only ones. 2.Init block executes every time before creating object of the class.

link :- https://www.youtube.com/watch?v=6qG3JE0FbgA&t=2s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!