Loading, connecting and initializing Java classes

JVM and classes

When calling a Java command to run a java program, the command will start a Java virtual machine process. No matter how many threads the Java program starts and how many variables it creates, they are all in the Java virtual machine process and share the memory area of the JVM process. The JVM process will be terminated when the following conditions occur in the system:

Class loading

When a program actively uses a class, if the class has not been loaded into memory, the system will initialize the class through three steps: loading, connecting and initialization (if there is no accident, the JVM will complete these three steps continuously, so sometimes these three steps are collectively referred to as class loading or initialization).

Class loading refers to reading the class file of a class into memory and creating a Java Lang.class object (note that it is not the object of the target class). That is, when any class is used in the program, a java.lang.class object will be created for it.

Class loading is completed by class loaders, which are usually provided by the JVM. These class loaders are also the basis for the operation of Java programs. These class loaders provided by the JVM are usually called system class loaders. In addition, developers can create their own class loader by inheriting the classloader base class.

By using different class loaders, you can load binary data of classes from different sources. There are usually several data sources as follows:

Java usually does not need to wait until the class is "used for the first time", and the Java virtual machine allows the system to pre load some classes.

Class connection

After the class is loaded, the system will generate a corresponding class object for it, and then enter the connection phase of the class.

The connection phase of the class is responsible for merging the binary data of the class into the JRE. Class connection can be divided into the following three stages:

Class initialization

In the class initialization phase, the virtual machine is responsible for initializing the class, mainly initializing the static attributes. There are two ways to initialize static attributes in Java classes:

When initializing, the JVM performs the initialization in the order in which the statements are arranged in the program. As shown in the following code, the final value of B is 9.

When the JVM initializes a class, it includes the following steps:

According to the above steps, when the program actively uses any class, the system will ensure that the class and all parent classes will be initialized.

Class initialization time

The time when the system starts initializing classes or interfaces includes the following six cases:

What should be mentioned is the static attribute modified by final. For example, if the static attribute modified by final gets the attribute value during compilation, the static attribute will be treated as a constant and will not be initialized (what does the compilation of the class do? It needs to be considered here). As follows:

If the static attribute modified by final fails to get the attribute value at compile time, it will be initialized, as follows:

It is also worth mentioning that the loadclass method of classloader does not perform class initialization, but only class loading.

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
分享
二维码
< <上一篇
下一篇>>