属性期间初始化静态字段。初始化类加载(加载、链接和初始化)的“阶段”,其中包括静态初始化器和其静态字段的初始化。静态初始化器按照类中定义的文本顺序执行。考虑一下这个例子: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"
}}b打印null因为当sayHello在静态作用域中调用的静态变量。a没有初始化。