Java class loading mechanism - class loading process (preparation)

The whole process of the Java virtual machine loads classes include: loading, validation, preparation, resolution, initialization. Validation, preparation, called to resolve the connection process. Today we are prepared to talk.

Preparation phase is formally allocate memory for class variables and set the stage class variable initial values ​​of these variables will be used by the memory allocation method in the region. This stage has two prone to confuse the concept of what needs to be emphasized. First of all, only this time the class variables (static variables modified), memory allocation. Not assigned to instance variables, instance variables in the java heap, is assigned when an object is instantiated as objects. Secondly, here the initial value "Normally" is the data type of zero value, assuming a class variable is defined as:

public static int value = 456;

Then the value after the preparatory phase of the initial value is 0, not 456. Because this time does not begin performing any java method, and the value 'putstatic instruction after the program is compiled, stored in the class constructor () method, the value assigned to the operation performed in the initialization phase.

It mentioned "normally" above the zero value, it must have a relatively lower "special case" is a non-zero value: If there is ConstantValue class attribute field attribute table field, that in the preparation stage variable value is initialized to ConstantValue specified attribute value, assuming the above value is defined as:

public static final int value =456;

When javac compiler will generate ConstantValue property value, the value will be assigned to the preparation phase 456.

Guess you like

Origin blog.csdn.net/huqianlei/article/details/90739024