Java static final field initialization order

我是研究僧i 提交于 2019-11-27 14:54:32

static final members are initialized before other static members.

non final static members 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 is initialized, so a=0 is displayed.

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

Regarding the edit in your question.

Looking at section 12.4.2 of the JLS :

  1. Then, initialize the final class variables and fields of interfaces whose values are compile-time constant expressions (§8.3.2.1, §9.3.1, §13.4.9, §15.28).

...

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

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

Static variables are initialized when the class gets loaded by class loader. So when first line “static Test t=new Test();” gets executed, the value of int “a” is not yet initialized, hence it is showing as 0. But other 3 cases (i.e. removing static, adding final or without any modifier) what happens a gets initialized at the time of Object creation of Test class, which is happening in the first line so it is showing the value “5”.

Java Language specification is best source to understand all about initialization order. According to that in your scenario, static final field gets initialized before any class level variable gets initialized. When you remove the final, initialization was deferred. 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();

it will also print

 a = 5
 a = 5

because of initialization order.

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

In first scenario main() method get executed first and it initializea to its default value 0.

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