Long intended to write a blog about class loading, because I have been troubled by this process for a long time, finally have time to write down some ideas today
There are many online articles about class loading, many are not rigorous, error, if you want to correct, more in-depth study, it is necessary to go to the official website to see the oracle of Java Virtual Machine Specification.
Path a little deep, along with the image below to find the top left corner
----------Into the title----------
Here we prepare a jclasslib bytecode viewer tool (GitHub inside), used to parse the bytecode files
After the Java file is compiled into bytecode (.class) file, you need to enter the virtual machine by loading this important part
The basic components:
Load - Links - initialization
Loading (Load)
Specification in this sentence: There are two kinds of classloaders:. The bootstrap class loader supplied by the Java Virtual Machine, and user-defined class loaders have been described two types loader (bootstrap class loader and the user-defined class), As the name suggests, the difference is whether inherited abstract class ClassLoader, these loaders have a superior-subordinate relationship, load follow parents delegate mechanism, which is loaded receives a load request is always first commissioned on a loader to load, if the parent can not be loaded and then loaded by themselves
The core API is loaded by the bootstrap class loader, for example, to load the java.lang.String class, bootstrap class loader receives the request, but they have a loader highest level, so the String class is loaded immediately
This step will generate a load Class object (Java reflection mechanism) are detailed specification
And pay attention to load a class will be loaded before its parent, here code demonstrates
public class LodingTest {
static class Father{
static {
System.out.println("加载父类中....");
}
}
static class Son extends Father{
static {
System.out.println("加载子类中...");
}
}
public static void main(String[] args) {
new Son();
}
}
Linking (Link)
Linking a class or interface involves verifying and preparing that class or interface, its direct superclass, its direct super interfaces, and its element type (if it is an array type), if necessary. Linking also involves resolution of
symbolic references in the class or interface, though not necessarily
at the same time as the class or interface is verified and prepared.
Summarized in three parts: verifying preparing resolution specification are explained in detail
That verification verifying bytecode files for virtual machine security considerations
preparing procedure as a class or interface to create static properties ie, class variables (class variables without a final modification, because the final static modification is a constant, the compiler stage has been processed) and static code block, and initializes the default value, but emphasize that this process do not need to wait until any instruction execution, the default initialization is not explicitly initialized explicitly initialized to perform the initializing stage
i.e. symbolic resolution analytical reference (symbolic reference), with a number of virtual machine instructions as anewarray, checkcast, getfield, getstatic ... related to the implementation, resolution is a very complex process, with the specification describes a long length ...
Initialization
Initialization stage of the implementation of a class or interface initialization method clinit, this method does not require us to write, and clinit name in java program is not a valid identifier, there is no way to write, this method of collecting the static action automatically assigned by a virtual machine and a static block is completed
Here we use the tools we mentioned above demonstrate
the code
public class LodingTest {
static int a = 1;
static {
double b = 0.5;
}
public static void main(String[] args) {
new LodingTest();
}
}
After generating the byte code file, using analytical tools
we can see the byte code clinit method of automatically generating a
class initialization is conditional, e.g., when C is initialized
to perform any relevant references C and a virtual machine instructions
initializing a subclass of C
if C is the interface and declares a non-abstract, non-static method, initialize a class C directly or indirectly
...
----------------------------------------------------------------
First wrote here, there is a need to add ...