Class loading

The Java files become.class files through the compiler and the class loader loads these.class files into the JVM and what the class loader does is essentially load the class and load the class means read the binary data from the class's.class file into memory and put it in the method area of the runtime data area A java.lang.Class object is then created in the heap to encapsulate the data structure of the Class in the method areaCopy the code

Where do I load the. Class file

(1) local disk (2) Online load. Class file (Applet) (3) from the database (4) zip file (ZAR, JAR, etc.) (5) generated from other files (JSP application)Copy the code

When does the class loader start?

In fact, Class loaders do not need to wait until a class is "first actively used" before loading it. The JVM specification allows classloaders to preload a class in anticipation of it being used if they encounter missing.class files or errors during preloading The class loader must not report a LinkageError until the program first actively uses the class. If the class is never actively used by the program, the class loader will not report an error.Copy the code

Second, the process of class loading

The process of class loading includes five stages: loading, verification, preparation, parsing and initialization

The loading, validation, preparation, and initialization phases occur in a certain order and the parsing phase is not necessarily the parsing phase which in some cases can start after the initialization phase and notice that the phases here start in sequence, It's not sequential or complete because these phases are often intermixed and often invoked or activated during the execution of one phaseCopy the code

1, load,

Loading is the first process of the class-plus mechanism. During loading, the virtual machine does three things:

(1) through a fully qualified name for the definition of a Class of binary byte stream (2) the byte stream represented by the method of static storage structure into the runtime data structure (3) in the heap generates a Class object representing the Class, as a method of the data access in the area Relative to the other phase of class loading, The loading phase is the most controllable phase because programmers can use the system's class loader to load, as well as their own class loader to loadCopy the code

2, validation,

The main function of validation is to ensure the correctness of the loaded class, which is also the first step in the connection phase. The.class file we loaded is not harmful to our virtual machine, so the verification is mainly completed in four stages:Copy the code
(1) Verification of file format:
Verify that the. Class file byte stream complies with the class file format specification and can be processed by the current version of the virtual machine. The main check magic number, the main version number, the main version number, etc. Class file contains data information, here can not understandCopy the code
(2) Metadata verification:
It mainly conducts semantic analysis on the information described by bytecode to ensure that the information described conforms to the requirements of Java language specifications, such as verifying whether the class has a parent class and whether the field methods in the class conflict with the parent class and so on.Copy the code
(3) Bytecode verification:
This is the most complex stage of the validation process. It is mainly through data flow and control flow analysis to determine that the program semantics are legitimate and logical. In the metadata verification stage to verify the data type after this stage is mainly to make analysis of the method of the class to ensure that the method of the class will not make weihai VIRTUAL machine security when running.Copy the code
(4) Symbol reference verification:
It is the final stage of validation that occurs when the virtual machine converts symbolic references to direct references. It mainly validates information outside the class itself. The goal is to ensure that the parsing action completes. The validation phase is an important but unnecessary phase for the entire class loading mechanism. If our code ensures that there are no problems, then there is no need to validate it, because validation takes time. Of course we can use -xverfity: None to turn off most validation.Copy the code

3, preparation,

The preparation phase focuses on allocating memory and setting initial values for class variables.

This memory is allocated in the method area. At this stage, we only need to pay attention to two key words: class variable and initial value: Class 1 variables (static) allocate memory but instance variables do not. Instance variables are allocated to the Java heap along with the instantiation of the object. Public static int Value = 1; public static int value = 1; // In this case, the value after the preparation phase is 0 instead of 1. // The action assigned to 1 is in the initialization phase.Copy the code

Other default values

Note that value is 0 after the prep phase when it is modified by static, but 1 after the prep phase when it is modified by both final and static. The compiler puts the result into the constant pool of the class that called itCopy the code

4, parsing,

The parsing phase is the process in which the virtual machine converts symbolic references in the constant pool into direct references.

What are symbolic applications and direct references? Symbolic reference: With a set of symbols to describe the referenced target can be any form of literal As long as it is could be unambiguous positioning to the target Like in the class, the teacher can use zhang SAN to represent you, you can also use your student id to represent you But no matter any way these are just a symbol (symbol) this code to your references (symbols) direct reference: A direct reference is a pointer to a target, a relative offset, or a handle that can be directly or indirectly located to the target, depending on how much memory the VM implements. A direct reference varies from vm to VM. The parse action is mainly for class or interface, field, class method, interface method, method type, method handle, and call point qualifier 7 class symbol referencesCopy the code

5. Initialization

This is the final step in the class-loading mechanism, where the Java program code actually executes. We know that class variables have been assigned once in the preparation phase. At the initialization stage, the programmer can assign values as he or she sees fit. In Java, there are two ways to initialize a class variable: ① Declare a class variable to specify an initial value; ② use a static code block to specify an initial value for a class variableCopy the code
JVM initialization steps
If the class has not yet been loaded and connected, the program will load and connect the class first. If the class has not yet been initialized, the system will initialize its immediate parent first. Initializing a class occurs only when a class is actively used, including the following six types: Create an instance of a Class, that is, access a static variable of a Class or interface as new, or initialize a subclass of a Class by calling the Class's static method reflection on that static variable (such as class.forname (" com.shengsiyuan.test ")). The parent class is also initialized. The Java VIRTUAL machine starts with a class that is identified as the startup class (JavaTest), and runs a main class directly using the java.exe commandCopy the code

Java program initialization order

1, static variable of the superclass 2, static code block of the superclass 3, static code block of the subclass 4, static code block of the subclass 5, non-static code block of the superclass 6, non-static code block of the superclass 7, constructor of the superclass 8, non-static variable of the subclass 9, non-static code block of the subclass 10, constructor of the subclassCopy the code

Class loaders

The virtual machine design team put the loading action outside the JVM implementation so that the application can decide how to get the required classes.

The Java language system comes with three class loaders:

The Bootstrap this: The top loading class, Jar, resources. Jar, charsets. Jar, and class in %JRE_HOME%\lib Also note that you can change the loading directory of the Bootstrap ClassLoader by specifying -xBootCLASSPath and path when starting the JVM. For example, Java -xBootclasspath /a:path appends the specified files to the default bootstrap path. We can open up my computer and look in the directory above to see if the jar packages exist in that directory. -d java.ext.dirs: Extention ClassLoader: jar packages and class files in the %JRE_HOME%\lib\ext directory can also be loaded to the directory specified by the -d java.ext.dirs option. Appclass Loader: also called SystemAppClass loads all classes of the current application's classpath. We see that Java provides us with three class loaders, all of which work together to load our applications, and we can add custom class loaders if we need to. What is the loading order of the three types of loaders? Bootstrap ClassLoader > Extention ClassLoader > Appclass LoaderCopy the code

The Bootstrap Loader is implemented in C language and cannot find a certain way to return the parent Loader, so null is returnedCopy the code

2. Three ways to load classes

(1) The main class containing the main() method is initialized by the JVM when the application is launched from the command line. Class. ForName (name,initialize,loader) initialze (Class. ForName (name,initialize,loader) initialze (Class. (3) Load dynamically through classloader.loadClass () method without executing initialization blockCopy the code

3. The principle of parental delegation

When a class loader receives a class loading task, it sends it to its parent class loader first so that the final loading task is passed to the top level boot class loader and only when the parent class loader fails to complete the loading task, One advantage of using parental delegation is that loading java.lang.Object in the rt.jar package, for example, is delegated to the top level launcher class loader regardless of which loader loads the class This ensures that different classloaders will end up with the same ObjectCopy the code
The principle of parental delegation can be summarized as follows:
It can avoid repeated loading. If the parent class has been loaded, the subclass does not need to be loaded again, which is safer and better solves the problem of unifying the base classes of each class loader. If this method is not used, users can define class loaders to load the core API at will, which will bring related risksCopy the code

4. Custom class loaders

(1) Follow the parent delegate model: inherit the ClassLoader and override the findClass() method. (2) Break the parent delegate model: inherit the ClassLoader and override the loadClass() method. The first approach is generally recommended for customizing class loaders that adhere to the parent delegate model to the greatest extent possible.Copy the code