Let's talk about the JVM class loading mechanism and jvm loading mechanism.

Source: Internet
Author: User

Let's talk about the JVM class loading mechanism and jvm loading mechanism.

I wrote this article a few months ago to deeply understand the Java class loading principle. I briefly talked about my understanding of Java class loading. Recently, I also studied the book "deep understanding of Java virtual machines, I have a more systematic understanding of the JVM class loading mechanism. Let's talk about it later.

How to load classes on virtual machines to prepare classes loaded into the memory

The class cannot be used directly after it is loaded into the memory. Basic preparation is required. During the preparation stage, the class variables (static variables or understood as global variables) perform the initial value assignment (this initial value is the "zero value" of the Data Type). The class variables are allocated in the method area, for introduction to the method area, see the analysis of JVM automatic memory management mechanism. However, assigning class variables to the values specified by the programmer is performed in the class.InitializationThis operation is located in the constructor of the class.<clinit>(). For example, codepublic static int value = 1218;, After the preparation stage, the value is 0, and after the initialization stage, the value is only 1218. However, if the final modifier is added to the value variable, the value in the preparation phase will be set to 1218.

Class initialization when to initialize the class

These methods are called active references to classes, and any other method of referencing a class will not trigger class initialization, which is called passive reference.

Initialization Process

Execution class Constructor<clinit>()This method is generated by the compiler automatically collecting the values of all class variables in the class and merging the statements in the static statement block. This method is different from the class Constructor (instance constructor ), constructors are used to create objects, while class constructors are used to create classes. In a multi-threaded environment, the virtual opportunity ensures that the method is correctly synchronized. In the same class loader, a class will only be initialized once. In my opinion, class initialization means creating a template for constructing an object, while object initialization means calling the constructor to construct an object.

Class Loader

As the name suggests, the class loader is used to load classes. Each class loader has its own class namespace, that is, the classes loaded by the two class loaders are not equal.Java class loading follows the parent-parent delegation model, that is, when a loader receives a request to load the class, it first delegates this work to the parent class loader for completion, when the parent class loader reports that it cannot complete the loading request (its search range does not find the desired class), The subclass will try to load it by itself.This mechanism ensures that the more basic classes are loaded by the upper class loader. Therefore, java. lang. Object is the same class in the multi-class loader environment. If you write a java. lang. Object, the class will not be loaded into the memory.

  • Start the Class Loader: BootStrap ClassLoader, which cannot be directly referenced by Java programs. to delegate a class loading request to it, you only need to use null. It is responsible for loading the core library of Java.
  • Extended Class Loader: Extension ClassLoader, used to load Java Extension libraries.
  • Application Class Loader: Application ClassLoader, also known as the system class loader, is used to load the class library specified on the user class path.

The above is a new understanding after reading the book. The book is more systematic and helpful for yourself to understand its principles. I just want to make a summary in this article, I hope I can do better in the future.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.