When are Static Variables Initialized in Java? – 静态变量在Java中何时初始化?

最后修改: 2020年 8月 1日

中文/混合/英文(键盘快捷键:t)

1. Introduction

1.绪论

In this tutorial, we’ll explore the static variable initialization process. The Java Virtual Machine (JVM) follows this process during class loading.

在本教程中,我们将探讨静态变量的初始化过程。在类的加载过程中,Java虚拟机(JVM)遵循这一过程。

2. Initialization Process

2.初始化过程

At a high level, the JVM performs the following steps:

在一个较高的水平上,JVM执行以下步骤。

First, the class is loaded and linked. Then, the “initialize” phase of this process processes the static variable initialization. Finally, the main method associated with the class is called.

首先,该类被加载和链接。然后,这个过程的 “初始化 “阶段处理静态变量的初始化。最后,与该类相关的main方法被调用。

In the next section, we’ll look at class variable initialization.

在下一节中,我们将看一下类变量的初始化。

3. Class Variable

3.班级变量

In Java, static variables are also called class variables. That is, they belong to a class and not a particular instance. As a result, class initialization will initialize static variables.

在Java中,静态变量也被称为类变量。也就是说,它们属于一个类,而不是一个特定的实例。因此,类的初始化将初始化静态变量。

In contrast, a class’s instance will initialize the instance variables (non-static variables). All the instances of a class share the class’s static variables.

相反,一个类的实例将初始化实例变量(非静态变量)。一个类的所有实例共享该类的静态变量。

Let’s take an example of class StaticVariableDemo:

让我们以StaticVariableDemo类为例。

public class StaticVariableDemo {  
    public static int i;
    public static int j = 20;

    public StaticVariableDemo() {}
}

First, the JVM creates a Class object for the class StaticVariableDemo. Next, the static field initializers assign a meaningful default value to the static fields. In our example above, the class variable i is first initialized with an int default value of zero.

首先,JVM为StaticVariableDemo类创建一个Class对象。接下来,static字段初始化器为静态字段分配一个有意义的默认值。在我们上面的例子中,类变量i首先被初始化为int默认值为0

The textual order applies to static fields. First, i will initialize and then j will be initialized. After that, the class and its static members will be visible to other classes.

文字的顺序适用于静态字段。首先,i将初始化,然后j将被初始化。之后该类和它的静态成员将对其他类可见。

4. Variable in a Static Block

4.静态块中的变量

Let’s take another example:

让我们再举个例子。

public class StaticVariableDemo {  
    public static int z;

    static {
        z = 30;
    }
    public StaticVariableDemo() {}
}

In this case, the variable initialization will be in sequence. For instance, the JVM initially assigns variable z to a default int value of 0. Then, in the static block, it is changed to 30.

在这种情况下,变量的初始化将按顺序进行。例如,JVM最初将变量z分配为默认的int值,然后,在static块中,它被改为30。

5. Variable in a Static Nested Class

5.静态嵌套类中的变量

Finally, let’s take an example of the nested class inside the outer StaticVariableDemo class:

最后,让我们举一个外层StaticVariableDemo类里面的嵌套类的例子。

public class StaticVariableDemo {  
    public StaticVariableDemo() {}
    
    static class Nested {
        public static String nestedClassStaticVariable = "test";
    }
}

In this case, the class StaticVariableDemo loads the Nested class. It’ll initialize the static variable nestedClassStaticVariable.

在这种情况下,类StaticVariableDemo加载Nested类。它将初始化静态变量nestedClassStaticVariable

6. Conclusion

6.结语

In this short article, we have briefly explained the static variable initialization. For further details, check the Java Language Specification.

在这篇短文中,我们简要地解释了静态变量的初始化。关于更多的细节,请查看Java语言规范

As always, the code snippets are available over on GitHub.

一如既往,代码片段可在GitHub上获得。