简体   繁体   中英

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? Then what happens in step 1 "when the class is loaded"?

 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:

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?

No, It's called construction of array. The initialization of array means putting things into it that you are doing after step 2.

Then what happens in step 1 "when the class is loaded"?

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. No memory is allocated to array at this point of time.

What happens till Step 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)

What happens after Step 2?

After Step 2 you are actually initializing the array ie putting the values in it.


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

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. If it found valid Java Code then it starts allocating memory for fields, methods, interfaces, etc. Create a reference to that memory locations. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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