Loading of Java classes

Keyworked Java class loading, class loading mechanism, classloader, parent delegation model, parent delegation model

We can see the sun every day. We get used to it for a long time. We don't think about how the sunlight comes. But if you think about this problem carefully, you may not be able to explain it clearly.

Similarly, we use the spring framework to develop java applications every day, slowly falling into the abyss of business development, becoming spring framework programmers and forgetting the essence of Java. For example, the loading of Java classes.

1. Who loads Java classes?

Loading a Java class means that it will Load the class file into the virtual machine. Loading is done by classloader and its subclasses.

Classloader is divided into:,

Bootstrap classloader is responsible for loading Java_ home/jre/lit/rt.jar

Extensionclassloader is responsible for loading some Java jars of extension functions_ home/jre/lib/ext/*. Jar corresponds to extclassloader

System classloader is responsible for loading classes and jars in classpath. The corresponding class is appclassloader

User define classloader user-defined classloader, such as bytecode encryption, user-defined calssloader to load and decrypt, inherit the classloader class and rediscover the findclass method.

Generally speaking, these four kinds of loaders form a parent-child relationship, and the upper level is the lower level parent loader.

When loading a class, you will first check whether the specified class has been loaded one by one from the bottom up. If it has been loaded, you will directly return the reference of the class.

If the specified class has not been loaded at the top level, it will try to load one by one from top to bottom until the user-defined class loader at the bottom level. If it fails, an exception will be thrown.

The hierarchical relationship of classloader is called "parent delegation model".

The user-defined classloader can realize the hot replacement of class.

2. When are Java classes loaded?

In short, when we want to use this class, it will load.

Initializing classes, reflecting classes, calling static methods of a class, etc

Note that a Class will only be loaded once, that is, only when the Person class method is first invoked, because Person has not been loaded before, so it is necessary to load the Person class.

3. How do I know which classes are loaded by the JVM?

To see which classes are loaded by the JVM, you can add the JVM parameter, - verbose: class. So there is output on the console.

Suppose there is a test class, and the pseudo code is as follows:

We add the - verbose: class parameter before running the class, so we can see the information printed by the method and the information loaded by the class on the console.

You can see the class loading information first, and then the method printing information executed by the class.

4. Steps of class loading

The process of class loading is divided into loading, linking and initialization (not required).

Loading is to find the binary bytecode and load it into the JVM. The JVM completes the class loading through the fully qualified class name and class loader.

The identification of the loaded class is: the fully qualified name of the class + classloader instance ID. Java virtual machine also judges whether two Java classes are the same based on this, that is, the fully qualified name of the class is the same and is loaded by the same classloader. Even if the same bytecode file is loaded by different classloaders, it will be considered as different classes.

Check the bytecode during linking. Whether the format is correct, whether the referenced class and attribute exist.

(throw exception if it does not exist, NoClassDefFoundError, nosuchmethoderror, nosuchfielderror)

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>