When objects created using a static link, why do the instance block and standard constructor execute before the static block?

public class TestLab { static Test aStatic=new Test(); public static void main(String[] args) { TestLab obj=new TestLab(); } static{ System.out.println("In static block of TestLab"); } } public class Test { static Test ref=new Test(); Test() { System.out.println("Default Constructor of Test"); } static { System.out.println("In Static Block of Test"); } { System.out.println("In instance block of Test"); } } 

Typically, static blocks are executed first during class loading. When the above example is executed, the following output is obtained:

In the instance block Test

Default Test Constructor

In a static testing unit

In the instance block Test

Default Test Constructor

In a static TestLab block

Why is the instance block and the standard constructor of the Test class run before the static block of the test class?

+6
source share
3 answers

Ok Fields / static blocks are set / executed during class initialization. They are executed in the order they appear in the code. So, after loading the TestLab class, when it is initialized, the following things happen:

  • static Test aStatic=new Test(); ==> Called as part of the initialization of the TestLab class. The class Test referenced from here. Thus, control moves to the Test class.

  • static Test ref=new Test(); ==> i, the first line of the Test class is executed (at the initialization stage). This line enables the creation of a new Test instance, so control passes to the instance block ( In the Test instance block ) Test , and then to the constructor ( Default Test Constructor ).

  • Now static Test ref=new Test(); is completed, so the initialization of the Test class continues and reaches a static block ( In a static test block ). This completes the initialization of Test .

  • The control returns to TestLab , now new Test() called. So, the Test instance block and the default constructor are printed again (the class is already initialized, so the static fields are not initialized again, and the static blocks are not executed).

  • Control reaches the static TestLab block ( In the static TestLab block ).

+7
source

When a type is initialized, all static initializers and all static field initializers are executed in textual order. From JLS 12.4.2 :

Then execute either initializers of the class variable, and initializers of the static class, or initializers of the interface fields, in textual order, as if they were a single block.

In other words, this code is executed:

 ref = new Test(); System.out.println("In Static Block of Test"); 

This first line creates an instance ... that requires starting the instance initializer. All initialization of this instance occurs before the control returns to the initialization part of the type - that is, before the line from the static initializer is started.

If you move the field declaration after the static initializer, you will see opposite results.

+1
source

Normally, static variables/blocks will be initialized in the order in which they are defined, here you marked aStatic as static . He will try to create an instance of Test by calling the constructor, but since the instance block is provided, it will execute, and then the constructor and, ultimately, the static block.

0
source

All Articles