简体   繁体   English

在java中初始化静态变量

[英]Initializing static variables in java

What exactly is meant by "Static variables are initialized when a class is loaded"? 究竟是什么意思“静态变量在加载类时被初始化”? I read lots of discussions available on the net but still I am confused. 我在网上阅读了很多讨论,但我仍感到困惑。 Step 2 is be the initialization step, right? 第2步是初始化步骤,对吧? Then what happens in step 1 "when the class is loaded"? 然后在第1步“加载类时”会发生什么?

 public class NewClass {
    static int[] arr; //Step 1
    NewClass(){
        arr = new int[10]; //Step 2
        for(int i= 0;i<10;i++){
            arr[i] = i;
        }
    }
}

If you want to initialize it when the class is loaded, then you should use the static initializer: 如果要在加载类时初始化它,则应使用static初始化程序:

public class NewClass {
    static int[] arr; //Step 1

    static {
        arr = new int[10]; //Step 2
        for(int i= 0;i<10;i++){
            arr[i] = i;
        }
    }
}

Initializing a static member in the constructor defeats the purpose of static members, since they don't belong to any instance, and each new instance you'll create will override the value of your static array. 在构造函数中初始化静态成员会破坏静态成员的用途,因为它们不属于任何实例,并且您将创建的每个新实例都将覆盖静态数组的值。

You should either initialize the static variable when it's declared or in a static initialization block. 您应该在声明静态变量时或在静态初始化块中初始化静态变量。

static int[] arr = new int[10];

or 要么

static {
    arr = new int[10];
}

The initialization (ie the execution of the static declarations and static initialization blocks) will occur when the class is loaded, which happens when your application first accesses any member (constructor, static method, static variable) of your class. 加载类时将发生初始化(即执行静态声明和静态初始化块),这在您的应用程序首次访问类的任何成员(构造函数,静态方法,静态变量)时发生。

Step 2 is be the initialization step, right? 第2步是初始化步骤,对吧?

No, It's called construction of array. 不,它被称为数组的构造。 The initialization of array means putting things into it that you are doing after step 2. 数组的初始化意味着在第2步之后将数据放入其中。

Then what happens in step 1 "when the class is loaded"? 然后在第1步“加载类时”会发生什么?

when the class is loaded all static variables are initialized with their default values. 加载类时,所有静态变量都使用其默认值进行初始化。 In case of Object it's default value is null or you can say a reference that is pointing to nothing. 对于Object它的默认值为null或者您可以说一个指向任何内容的引用。 No memory is allocated to array at this point of time. 此时没有为阵列分配内存。

What happens till Step 2 ? 在第2步之前会发生什么?

When the object of type NewClass is created using keyword new at that time constructor is called and the array is constructed and memory is assigned for 10 int values in the heap with all zero as default value (till step 2) 当使用关键字new创建NewClass类型的对象时,将调用构造函数并构造数组并为堆中的10个int值分配内存,并将所有零作为默认值(直到步骤2)

What happens after Step 2? 第2步后会发生什么?

After Step 2 you are actually initializing the array ie putting the values in it. 在第2步之后,您实际上正在初始化数组,即将值放入其中。


static int[] arr;   // declaration

arr = new int[10];  // construction

arr[i] = i;         // initialization

If you want to read more about it then read book SCJP Sun Certified Programmer for Java 6 如果您想了解更多信息,请阅读SCJP Sun认证程序员Java 6

When the class is loaded by Class Loader, the job of linker starts. 当类加载器加载类时,链接器的作业将启动。 Linker verifies the Java code, whether it is written as per specifications given in Java Language & JVM. 链接器验证Java代码,它是否按照Java语言和JVM中给出的规范编写。 If it found valid Java Code then it starts allocating memory for fields, methods, interfaces, etc. Create a reference to that memory locations. 如果找到有效的Java代码,则它开始为字段,方法,接口等分配内存。创建对该内存位置的引用。 Once reference is assigned to memory location, all field variables, methods, interfaces, etc are initialized to there default values, if not specified explicitly. 将引用分配给内存位置后,如果未明确指定,则将所有字段变量,方法,接口等初始化为默认值。 Otherwise, it assigns whatever value is set as its initial value. 否则,它会将设置的任何值指定为其初始值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM