Detailed Java Virtual machine class loading

Source: Internet
Author: User

Overview

In the Java language, the loading, connection, and initialization of types are done during program runs. The virtual machine loads the data of the description class from the class file or other place into memory, verifies the data, parses and initializes it, and finally forms the Java type that can be used directly by the virtual machine, which is the class loading process of the virtual machine.

time of class loading

The order of the 5 phases of loading, validating, preparing, initializing, and unloading is determined, and the class's loading process must begin in this order, and the parsing phase is not necessarily: it can start after the initialization phase in some cases. These phases are usually mixed in a cross-section, usually invoking and activating another phase during one phase of execution.

The Java Virtual Machine specification does not enforce constraints to start the class loading process. However, for the initialization phase, the virtual machine specification is strictly defined and there are only 5 cases where the class must be initialized immediately:

    1. When instantiating an object using the New keyword, when reading or setting a static field of a class, and when invoking a static method of a class.
    2. When you use the Java.lang.reflect package method to make a reflection call to a class.
    3. When initializing a class, it is necessary to trigger the initialization of its parent class if it finds that its parent class has not yet been initialized.
    4. When the virtual machine is started, the user needs to specify a main class to execute (the main class that contains the main () method), and the virtual opportunity initializes the main class first.
    5. When using dynamic language support for JDK1.7, if a java.lang.invoke.MethodHandle instance finally resolves to a method handle of ref_getstatic/ref_invokestatic/ref_putstatic , and the class that corresponds to the method handle is not initialized, you need to trigger its initialization first.

In addition to these 5 scenarios, all methods of referencing a class do not trigger initialization.

the process of class loading

I drew a mind map to describe the process, such as:

If the browser does not see clearly, save to the computer on the open picture.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Detailed Java Virtual machine class loading

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.