java 类中的静态块和静态变量以什么顺序执行?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12448465/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
in what order are static blocks and static variables in a class executed?
提问by tr_quest
Possible Duplicate:
Java static class initialization
可能的重复:
Java 静态类初始化
Why is the string variable updated in the initialization block and not the integer(even though the block is written first)
为什么在初始化块中更新字符串变量而不是整数(即使先写入块)
class NewClass
{
static
{
System.out.println(NewClass.string+" "+NewClass.integer);
}
final static String string="static";
final static Integer integer=1;
public static void main(String [] args)//throws Exception
{
}
}
My output is
我的输出是
static null
P.S:Also noticed that string variable initialization happens before the block only when i insert the final modifier. why is that?why not for integer as well?I have declared it as final static too
PS:还注意到只有当我插入最终修饰符时,字符串变量初始化才会在块之前发生。为什么会这样?为什么不是整数?我也将其声明为最终静态
回答by Jon Skeet
From section 12.4.2of the JLS, snipped appropriately:
从JLS 的第 12.4.2 节中,适当地剪下:
The procedure for initializing C is then as follows:
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).
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.
初始化 C 的过程如下:
然后,初始化最终类变量和接口的字段,其值为编译时常量表达式(第 8.3.2.1 节、第 9.3.1 节、第 13.4.9 节、第 15.28 节)。
接下来,按文本顺序执行类的类变量初始值设定项和静态初始值设定项,或接口的字段初始值设定项,就好像它们是单个块一样。
So for non-compile-time-constants, it's not a case of "all variables" and then "all static initializers" or vice versa - it's all of them together, in textual order. So if you had:
因此,对于非编译时常量,它不是“所有变量”然后“所有静态初始化程序”的情况,反之亦然——它们全部按文本顺序放在一起。所以如果你有:
static int x = method("x");
static {
System.out.println("init 1");
}
static int y = method("y");
static {
System.out.println("init 2");
}
static int method(String name) {
System.out.println(name);
return 0;
}
Then the output would be:
那么输出将是:
x
init 1
y
init 2
Even making x
or y
final wouldn't affect this here, as they still wouldn't be compile-time constants.
即使是 makex
或y
final 也不会对这里产生影响,因为它们仍然不是编译时常量。
P.S:Also noticed that string variable initialization happens before the block only when i insert the final modifier.
PS:还注意到只有当我插入最终修饰符时,字符串变量初始化才会在块之前发生。
At that point, it's a compile-time constant, and any uses of it basically inlined. Additionally, the variable value is assigned before the rest of the initializers, as above.
在这一点上,它是一个编译时常量,它的任何使用基本上都是内联的。此外,变量值在其余初始化程序之前分配,如上所述。
Section 15.28of the JLS defines compile-time constants - it includes all primitive values and String
, but notthe wrapper types such as Integer
.
JLS 的第 15.28 节定义了编译时常量 - 它包括所有原始值和String
,但不包括包装类型,例如Integer
.
回答by Kumar Vivek Mitra
Here is a short and straight forward answer to you question....
这是对您问题的简短而直接的回答......
static Variable
:
static Variable
:
static Variables are executed when the JVM
loadsthe Class
, and the Class
gets loaded wheneither its been instantiatedor its static method
is being called.
当静态变量执行JVM
负荷的Class
,而Class
当被加载或者其被实例化或者它static method
被调用。
static Block or static Initializer Block
:
static Block or static Initializer Block
:
static static Initializer Block gets Initialized beforethe Class
gets instantiatedor beforeits static method
is called, and Even beforeits static variable
is used.
静态静态初始化块被初始化之前的Class
被实例化或之前它static method
被调用,即使在它static variable
被使用。
///////// Edited Part /////////
///////// 编辑部分 /////////
class NewClass {
final static String string = "static";
final static Integer integer = 1;
static {
System.out.println(NewClas.string + " " + NewClas.integer);
}
public static void main(String [] args) { // throws Exception
new NewClas();
}
}
The above will printstatic 1
.
以上将打印static 1
。
The reason is that the JVM
will do the optimization processknown as Constant folding
, doing an pre-calculation of the constant variables.
原因是JVM
将执行称为的优化过程Constant folding
,对常量变量进行预计算。
Moreover in your case the resultwas static null
cause Constant folding
is applied to Primitive type and not Wrapper Object, in your case its Integer...
此外,在你的情况下,结果是static null
导致Constant folding
被应用到原始类型,而不是包装对象,在你的情况下,其整...
回答by Ivan Koblik
They are initialized in the given order (fields and static blocks), that's why printed value is null
, nothing was assigned to static fields that are defined after the static block.
它们以给定的顺序(字段和静态块)初始化,这就是为什么打印值是null
,没有分配给静态块之后定义的静态字段。