Depth understanding JVM- class loader initialization phase - active and passive reference to the class

JVM class loading phase of the initialization phase P210

  Five cases specified virtual machine must be "initialized" The situation of the class

  1. encountered new, when getstatic, putstatic, or invokestic four byte code instructions, if the class has not been initialized, you need to initialize it triggered, when the most common java code to generate this scene four instructions: using the new keyword when the object is instantiated, read or set a static field of a class (the final modification, the compiler has been the result into the constant pool except for static fields), when a static method invocation, and class time.

  2. When using java.lang.reflect package approach to reflect class called, if there has been no class initialization is required to trigger its initialization.

  3. When a class is initialized, the palace found that its parent has not been initialized, you need to trigger initialization of the parent class.

  4. When the virtual machine starts, the user needs to specify a main class to be executed (contains main () method of that class), the virtual machine to initialize this master class.

  5. When using a dynamic language support JDK7 if a final example of the analysis result java.lang.invoke.MethodHandle REF_getStatic, REF_putStatic, REF_invokeStatic the method handle, and this handle class corresponding method not been initialized, it is necessary to trigger its initialization.

 

This five cases ------ belong to a class to be active references .

  In addition, all references to class methods are not trigger initialization, become passive reference

  A reference a static field of the parent class by subclass does not cause class initialization word

public class SuperClass {
public static int value = 123;
static {
System.out.println("SuperClass init!!!");
}
}
public class SubClass extends SuperClass {
static {
System.out.println("SubClass init!!");
}
}
{class InitTest public 
public static void main (String [] args) {
System.out.println (SubClass.value);
}
}
// output
// static subclass traversal call parent class, so that the parent class initialization is triggered, but the sub- class initialization is not called

 

 

You can print out the class load order can be used to troubleshoot conflicts of class:

-XX:+TraceClassLoading

  Second, the reference to an array of class by definition, does not trigger this type of initialization

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

  Third, the constants at compile time will be credited to the constant pool class is called, there is no direct reference to the definition of the class constants of nature, it will not trigger initialization constants defined in class

public class ConstClass {
static {
System.out.println("ConstClass init!!!");
}
public static final String HELLOWORLD = "hello world";
}
public static void main3(String[] args) {
System.out.println(ConstClass.HELLOWORLD);
}

  

 

Guess you like

Origin www.cnblogs.com/huan30/p/11774102.html