Initialization Procedure for Java Static Finite Field

I tried to understand the behavior of the initialization order when static fields are initialized with a reference to the same object of the wrapper class.

public class Test { static final Test t=new Test(); static int a=5; Test(){ System.out.println("a="+a); } public static void main(String[] args) { new Test(); } } 

The output above the code snippet:

 a=0 a=5 

If I change the variable a to something other than a simple static :

 static final a=5; a=5; final a=5; 

Output:

 a=5 a=5 

Why is this behavior?

Note that the output is a=5 & a=5 , even if both t & a declared as static final , in which case t precedes the declaration of a

0
java constructor static final static-members
source share
4 answers

static finite elements are initialized before other static members.

non-finite static elements are initialized in order of appearance

Therefore, in your first case:

  static Test t=new Test(); static int a=5; 

The constructor is first called before a initialized, so a=0 displayed.

In the second case, static final a initialized to t , so a=5 displayed when the first instance of Test . When a not static, it is initialized before the constructor runs, so a=5 displayed again.

Regarding editing in your question.

In section 12.4.2 of the JLS :

  1. Then initialize the final class variables and interface fields whose values ​​are constant compilation time constant (§8.3.2.1, §9.3.1, §13.4.9, §15.28).

...

  1. Next, execute either the class variable initializers, or the static class initializers, or interface field initializers, in textual order, as if they were a single block.

You see that class final variables (i.e. static final) are initialized before the rest of the static variables only if their values ​​constitute compilation time constant expressions . 5 is a constant expression. new Test() not. Therefore, a initialized to t , even if both are static.

+2
source share

Static variables are initialized when the class is loaded by the class loader. Therefore, when the first line is "static Test t = new Test ();" gets the value int "a" has not yet been initialized, therefore, it is displayed as 0. But the other 3 cases (for example, removing statics, adding the final or without any modifier) ​​that occurs, gets initialized during the creation of the Test Class object, which happens on the first line, so that it shows the value "5".

+1
source share

The Java language specification is the best source for understanding the entire initialization order. According to your scenario, the static final field is initialized before the class level variable is initialized. When you delete the final, initialization has been delayed. It should also be noted if you change

 static Test t=new Test(); static int a=5; 

to

  static int a=5; static Test t=new Test(); 

he will also print

  a = 5 a = 5 

due to the initialization order.

+1
source share

static final a=5 This is final , so it is initialized first before other static members or methods.

In the first scenario, the main() method is executed first, and initializes a its default value of 0 .

0
source share

All Articles