Java Virtual Machine class loading mechanism - the class loader occasion

The virtual machine data that describes the classes loaded from Class file into memory, and verify the data, and converts parsing initialization, forming Java type can be used as a virtual machine, which is the class loading mechanism of the virtual machine .

In the JVM class life cycle:
Load -> verify -> Preparation -> Analytical -> Initialization -> use -> Uninstall
wherein verify -> Preparation -> Analytical connecting portion collectively.

Under what circumstances would it be loaded? The JVM specification does not the obligation, to specific virtual machine can be freely grasp. But there are five cases must be immediately initialize class:
1, encountered new, getstatic, putstatic, when invokestatic these four byte code instructions, if you have been initialized within a class, you need to trigger their initial. This generates bytecode instructions four most common scenario is: new new use, to read or set a static field of a class (the final modification, except for the results have been compiled into the constant pool of static fields) of time, and calling when a static method of a class.
When 2, java.lang.reflect package approach to reflect the class called, if the class is not been initialized, you need to trigger its initialization.
3, when a class is initialized, if we find its parent not been initialized, you need to trigger initialization of the parent class.
4, when the virtual machine is started, users need to specify a main class to be executed (contains main () method of that class), the virtual machine to initialize this master class.
5, when the dynamic language support JDK1.7, if a method handle java.lang.invoke.MethodHandle instance final analysis result REF_getStatic, REF_putStatic, REF_invokeStatic, and this method handle corresponding to the class not been initialized, You need to trigger its initialization.

The following three examples:
Example 1

public class SubClass extends SuperClass {
//子类
    static {
        System.out.println("SubClass init!");
    }
}
public class SuperClass {
//父类
    static {
        System.out.println("SuperClass init");
    }
    public static int value = 123;
}
public class test {
    public static void main(String[] args){
    //通过子类调用父类的静态字段,不会进行初始化
        System.out.println(SubClass.value);
    }
}

The output of the code is
the SuperClass the init
123

The reason: For static fields, only direct define the class will initialize the field, and therefore referred to by its subclasses static fields defined in the parent class, it will only trigger initialization of the parent class, without triggering initialize the subclass.

Example 2
followed embodiment SuperClss Class 1

public class test {
    public static void main(String[] args){
        SuperClass[] a = new SuperClass[10];
    }
}

Not output SuperClass init. Means SuperClass not been initialized.

Cause: This code will make a inherit Object class is initialized. Initializing class is a one-dimensional array generated by the JVM automatically, directly inherited from Object, create an action triggered by newarray.

Example 3

public class Const {

    static {
        System.out.println("ConstClass init!");
    }
    static {
        System.out.println("ConstClass init 222!");
    }

    public Const(){
        System.out.println("调用Const的构造函数");
    }

    public static final String HW = "HelloWorld";
}

public class NoInitialization {
    public static void main(String[] args){
        System.out.println(Const.HW);
    }
}

Output: HelloWorld
This means Const not initialized

The reason: While references constants Const class in java source code, but in fact at compile time, through constant propagation optimization, this has been kept constant to a constant pool NoInitialization class, after NoInitialization reference to the constant Const.HW are into constant reference to itself. That, in fact NoInitialization the class file and there is no sign Const reference to the class entrance.

Loading the interface with the class loader is slightly different. Interfaces initialization, but differs in that the interface must be initialized scene, a third class that must be initialized with different scenarios: when the class initialization is performed, all the requirements of the parent class has been initialized; but not an interface, the interface does not require parent all have been initialized, the only real parent interface to use when (constants defined in the interface, such as references) will be initialized.

Guess you like

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