the loading process of the class
JVM divide the class loading process into three steps
Loading, connection, initialization, and connection are divided into three steps as follows:
JVM The class loading process is divided into three steps: load ( Load), linking (link) and initialization (Initialize) The link is also divided into three steps, as shown in:
1) load: Finds and loads the binary data of the class;
2) Links:
- Validation: Ensure that the class being loaded is correct;
- Prepare: Allocates memory for static variables of the class and initializes them to default values;
- Parse: Converts a symbolic reference in a class to a direct reference;
3) Initialize: Assigns the correct initial value to the static variable of the class;
Then why should I have to verify this step? First, if the compiler generates class file, it must be compliant with JVM class file, let JVM load and run for malicious purposes, it is not good, so this class file to verify this first pass, do not meet the words will not let it continue to do, but also for security reasons.
The prep phase and initialization phase seem a bit Shang, in fact, not Shang, if there are statements in the class:private static int a = ten, it's the execution process is like this, first the bytecode file is loaded into memory, first link verification This step, verify through the post-preparation phase, toaallocate memory because the variableais aStatic, so at the momentaequalsintDefault initial value of type0, i.e.a=0,then to the parsing (later on), to initialize this step, only theathe true value of theTenassigned toA,at thisa=10.
initialization of the class
When the class is initialized:
- 1 ) To create an instance of the class, i.e. New an Object
- 2 ) to access a static variable for a class or interface, or to assign a value to the static variable
- 3 ) Call the static method of the class
- 4 ) Reflection ( class.forname ("Com.lyj.load") )
- 5 Initializes a subclass of a class (the parent class of the child class is initialized first)
- 6 ) JVM Startup class indicated at startup, that is, the file name and class name are the same class
only these 6 cases will cause the class's class to be initialized.
Loading of Classes
when starting the JVM , you can use three classloader: boot (bootstarp) class loader, extension ( Extensions) class loader, application (application) class loader.
boot class Loader: only responsible for loading the core java Libraries, such as those located in <java_home>/jre/lib vm.jar core.jar JVM native code written.
extension class Loader: Responsible for loading code under extension path , typically located in <java_home>/jre/lib/ext or via java.ext.dirs This system property specifies the code under the path. This classloader is made up of sun.misc.launcher$extclassloader implemented.
To apply the class loader: in general, Java the classes that are applied are all loaded by it. It can be obtained by Classloader.getsystemclassloader () .
If the application class loader needs to load a class, it first delegates the extension class loader, and the extension class loader then delegates the bootstrap class loader. If the parent loader cannot load the class, the subclass loader returns to its own library to find the class. Based on this feature, the ClassLoader is responsible only for classes that its ancestors could not load.
Summary:
the class loader is Java of an innovation, basically all classloader are used java. An instance of the Classloaderler load. The basic purpose is to help us understand the instantiation process of objects and the invocation of objects.
Java class Loader