Class loading mechanism
The virtual machine data that describes the classes loaded from Class file (which can be present in any form) into memory, and verify the data, and converts parsing initialization, forming Java type can be used as a virtual machine, the virtual machine that is class loading mechanism.
Java can dynamically expand in the natural language features that rely on dynamic loading and dynamic linking run this feature implemented.
The life cycle
Classes begin to be loaded into the virtual machine memory, up to unload the memory of his entire life cycle, including:
Note: load, validation, preparation, initialization sequence and unload these five stages are determined.
Is not necessarily resolved: it can be re-started after the initialization phase, in some cases, this is the time to run Java language bindings.
Class initialization time
有且仅有
The following five cases require the class is initialized (loading, validation, preparation needs to start before this):
- Encounter new, getstatic, putstatic invokestatic or 4 when this bytecode instruction, if the class is not been initialized, initialization is required to trigger it. Corresponding Java code level: new instance of an object, to read or set a static variable (non-final modification), call the static method.
- Use
java.lang.reflect
when the package approach to reflect the class called, if there is no need to initialize the triggers its initialization. - When a class is initialized, if the parent class has not been initialized, it is performed.
- When the virtual machine starts, you need to perform a master class, you will first initialize the main class.
- When using a dynamic language support JDK1.7 if an
java.lang.invoke.MethodHandle
instance final analysis resultREF_getStatic
,REF_getStatic
,REF_getStatic
the method handle, and this method handle class corresponding not been initialized, initialization is required to trigger it.
These five, called the initiative a reference to a class; outside divided by reference to the class method will not trigger initialization, known as passive reference.
Description of the initialization interface
Loading interface and class loading process a little bit different interfaces initialization process, although the interface is not static静态语句块
, but the compiler will still generate an interface <cinit>()类构造器
for initializing member variables defined in the interface.
Interfaces and classes really differ is in front of the third kind in five cases: 当一个类在初始化时,要求父类全部初始化,但是一个接口在初始化时,并不要求其父接口全部完成了初始化,只有在真正使用到父接口的时候(如引用接口中定义的常量)才会初始化
.
Passive referenced topic
The following cases, it may be a pen questions.
Subclass reference a static variable of the parent class
public class SuperClass{
static{
System.out.println("SuperClass init!");
}
public static int value = 123;
}
public class SubClass extends SuperClass{
static{
System.out.println("SubClass init!");
}
}
/**
* 非主动使用类字段
**/
public class NotInitialization{
public static void main(String[] args){
System.out.println(SubClass.value);
}
}
Analysis: The above code will output "SuperClass init!", But not output "SubClass init!".
对于静态字段,只有直接使用定义这个字段的类才会被初始化
Therefore reference to the static field in the parent class defined by its subclasses, it will only trigger initialization of the parent class without triggering initialize the subclass.
Array reference
public static void main(String[] args){
SuperClass[] sca = new SuperClass[10];
}
Analysis: no output "SuperClass init!", Explained SuperClass did not trigger the initialization phase, but there triggered the [L*.*.*.SuperClass
initialization phase of the class, which is automatically generated by the virtual machine, create an action by the byte code newarray
is triggered.
Static variables referenced final show
public class ConstClass{
static{
System.out.println("ConstClass init!");
}
public static final String HELLOWORLD = "hello world!";
}
public class NotInitialization{
public static void main(String[] args){
System.out.println(ConstClass.HELLOWORLD);
}
}
Analysis: no output "ConstClass init!", Explained the class has not initialized. This is because the final modification of the constants at compile phase 常量传播优化
, this has been a constant value hello world!
stored in the NotInitialization
constant pool of the class, after NotInitialization of the constant ConstClass.HELLOWORLD
references to references are converted to actual NotInitialization class of its own constant pool.