The introduction
This is the third day of my participation in the November Gwen Challenge. Check out the details: the last Gwen Challenge 2021
For a long time, I am committed to spread the positive energy, I wrote it with my own style, his own ideas, not to easy, plagiarism, to cater to others, I based on facts, story of my abilities in my ability range, I think can do it very well, if you write the article is not good, I also have no a way, trying to do.
In this article you will learn about the life cycle of a class, how the loading process starts in the virtual machine, what the process looks like, and what it does for you.
The start of the class
Start of class in the specification is described as the initial virtual machine and start a class through the start class loader to specify the initial class started, (the current version of the specification is java8), and then links and initialize the class, after will call public method is the main method, also is the entrance, we program to drive the operation of the entire program.
As for the choice of initial classes, you can either provide them as parameters, or you can implement them yourself and provide a class loader that loads the application. Other options are also available, as long as you meet the specifications above.
Class life cycle
The life cycle of a class is also an old topic: load, validate, prepare, parse, initialize, use, unload.
- Load: find the byte stream, in other words, find our Class file.
- Validation: See if the class file conforms to the parse format
- Preparation: Allocates memory for static fields of the loaded class
- Parsing: To parse symbolic references into actual references.
- Initialization: Primarily initializes the constructor
Creation of a class or interface
The creation of a class or interface requires the creation of a matching internal representation in the method area. The creation of a class or interface is triggered by another class or interface, and can be triggered by Java libraries such as reflection.
Java class libraries are as follows:
- Reflection, such as the classes in the package
java.lang.reflect
and the classClass
. - Loading and creation of a class or interface. The most obvious example is the class
ClassLoader
. - Linking and initialization of a class or interface. The example classes cited above fall into this category as well.
- Security, such as the classes in the package
java.security
and other classes such asSecurityManager
. - Multithreading, such as the class
Thread
. - Weak references, such as the classes in the package
java.lang.ref
.
Then, see if the class is an array class, and if not, use itClass loaderTo load its binary representation.
Class loader
Class loader type
There are three types of class loaders: startup class loaders, extension class loaders, and application class loaders. Start the class loader to load the core Java libraries, such as the Rt. jar package in the JRE. The extension class loader loads the package in the lib directory, ext, and the rest is loaded by our application class loader, such as our own classes.
In addition, you can customize class loaders to load specific classes.
Java virtual machines support two types of loaders: provided by Java virtual machinesStart the class loaderandUser-defined class loaderEach user-defined Classloader should be an instance of a subclass of the abstract Classloader class. The purpose of providing users with their own class loaders is to extend the functionality of the Java virtual machine to support dynamic loading and creation of classes.
The loading mechanism of the class loader
Class loaders are designed to ensure that a class is only loaded once. For C, you can create it yourself or delegate it to another class loader. If you do it yourself, you define and create the class.
If you delegate load requests, then you didn’t define the class directly, so you can say that you created it initially. You can see that the specification says you can delegate, but it doesn’t say exactly how to delegate, so you can implement it internally.
Let’s move on: if the class is defined by the bootstrap loader, then it is loaded with the bootstrap loader, if it is user-defined, then it is loaded with the user-defined class loader, and if N is an array class, then the array class was created by the Java Virtual machine rather than the class loader.