Java 静态类初始化何时发生?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3499214/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-14 01:19:43  来源:igfitidea点击:

When does static class initialization happen?

javastaticinitialization

提问by Tony R

When are static fields initialized? If I never instantiate a class, but I access a static field, are ALL the static blocks and private static methods used to instantiate private static fields called (in order) at that instant?

什么时候初始化静态字段?如果我从不实例化一个类,但我访问了一个静态字段,那么所有用于实例化私有静态字段的静态块和私有静态方法是否都在那个时刻被调用(按顺序)?

What if I call a static method? Does it also run all the static blocks? Before the method?

如果我调用静态方法怎么办?它还运行所有静态块吗?之前的方法?

采纳答案by Stephen C

A class's static initialization normally happens immediately before the first time one of the following events occur:

类的静态初始化通常在以下事件之一第一次发生之前立即发生:

  • an instance of the class is created,
  • a static method of the class is invoked,
  • a static field of the class is assigned,
  • a non-constant static field is used, or
  • for a top-level class, an assert statement lexically nested within the class is executed1.
  • 创建了一个类的实例,
  • 调用类的静态方法,
  • 分配了类的静态字段,
  • 使用非常量静态字段,或
  • 对于顶级类,执行词法嵌套在类中的 assert 语句1

See JLS 12.4.1.

参见JLS 12.4.1

It is also possible to force a class to initialize (if it hasn't already initialized) by using Class.forName(fqn, true, classLoader)or the short form Class.forName(fqn)

也可以通过使用强制类进行初始化(如果尚未初始化)Class.forName(fqn, true, classLoader)或短表Class.forName(fqn)



1 - The final bullet point was present in the JLS for Java 6 through Java 8, but it was apparently a mistake in the specification. It was finally corrected in the Java 9 JLS: see source.

1 - 最后一个要点出现在 Java 6 到 Java 8 的 JLS 中,但这显然是规范中的一个错误。它最终在 Java 9 JLS 中得到纠正:请参阅源代码

回答by Nikita Rybak

Yes, all static initializers are run before you access class first time. If it was any other way, I would call it a bug.

是的,所有静态初始值设定项都在您第一次访问类之前运行。如果是其他任何方式,我会称其为错误。

回答by naikus

Static fields are initialized during the initialization"phase" of the class loading (loading, linking and initialization) that includes static initializers and initializations of its static fields. The static initializers are executed in a textual order as defined in the class.

静态字段在类加载(加载、链接和初始化)的初始化“阶段”期间被初始化,该阶段包括静态初始化程序和其静态字段的初始化。静态初始值设定项按类中定义的文本顺序执行。

Consider the example:

考虑这个例子:

public class Test {

   static String sayHello()  {
      return a;
   }

   static String b = sayHello(); // a static method is called to assign value to b.
                                 // but its a has not been initialized yet.

   static String a = "hello";

   static String c = sayHello(); // assignes "hello" to variable c

    public static void main(String[] arg) throws Throwable {
         System.out.println(Test.b); // prints null
         System.out.println(Test.sayHello()); // prints "hello"
    }
}

The Test.b prints nullbecause when the sayHellowas called in static scope, the static variable awas not initialized.

Test.b 打印null是因为sayHello在静态范围内调用 时,静态变量a未初始化。