[Reading notes] Java class loading process

Source: Internet
Author: User

I. Life cycle of a class

Class starts from being loaded into the virtual machine memory, and has the following () life cycles until the memory is unloaded:



This is called the load process for the class, resolution-- --------
The Java Virtual Machine specification does not impose constraints on when it is necessary to start the first phase of class loading, but instead gives the virtual machine a grasp of the specific implementation.
However, for the initialization phase, the virtual machine has the following 5 cases where the class must be "initialized" immediately:
(1) When encountering the 4 bytecode instructions of new, getstatic, putstatic or invokestatic, it is necessary to trigger its initialization immediately if the class is not initialized. Common scenario: Use new to instantiate an object, read or set a static field for a class (in addition to the final field that the compiler has processed), and to invoke a static method of a class.
(2) The initialization of a class is also triggered when a class is called with Reflection (Java.lang.reflect package).
(3) When a class is initially initialized, the initialization of the parent class is triggered first when it is discovered that the parent class is not initialized.
(4) When the virtual machine starts, the user needs to specify a master class that executes the main method and initializes the class with a virtual opportunity.
(5) When using JDK1.7 's dynamic language support, if a java.lang.invoke.MethodHandle instance finally resolves the results ref_getstatic, ref_putstatic, ref_ The Invokestatic method handle that triggers the initialization of the corresponding class.
The behavior of the above 5 scenarios is called an active reference to a class, except that the reference will not trigger the initialization of the class, known as a passive reference.

The following is an alternative to a passive reference:

  
 
  1. class SuperClass
  2. {
  3. static
  4. {
  5. System.out.println("SuperClass init!");
  6. }
  7. public static int value = 123;
  8. }
  9. class SubClass extends SuperClass
  10. {
  11. static
  12. {
  13. System.out.println("SubClass init!");
  14. }
  15. }
  16. public class NotInitialization
  17. {
  18. public static void main(String[] args)
  19. {
  20. System.out.println(SubClass.value);
  21. }
  22. }

Output Result:
Superclass init!
123

Two. Loading process of Class 1. Loading

During the load phase:
(1) obtain a binary byte stream that defines this class by the fully qualified name of a class;
(2) Transform the static storage structure represented by this byte stream into the runtime data structure of the method area;
(3) Generate a Java.lang.Class object representing this class in memory as the access entry for various data of this class in the method area.

2. Verification

Validation is to ensure that the byte stream contains information that conforms to the requirements of the current virtual machine and is secure.
There are about four stages of verification
(1) file Format verification: Verify that the byte stream conforms to the specification of the class file format;
(2) meta-data validation: The semantic analysis of the information of bytecode description, the purpose is to check the metadata information (data type) of the class;
(3) Bytecode verification: Validation analysis of the class method body;
(4) Symbolic reference validation: matching checks on information outside the class itself (various symbol references in a constant pool).

An optimization can be made here: because the validation phase is time-consuming or very large, if it has been reused multiple times and is validated code can skip this step and speed up the class loading.

3. Preparation

Formally allocates memory for class variables (static) and sets the initial values of class variables, where the class variables are final decorated.

4. Parsing

The process of replacing a symbolic reference with a direct reference.

5. Initialization

The process of executing the () class construction method, which is the execution of the static block statement and the copy operation of the variable.



From for notes (Wiz)

[Reading notes] Java class loading process

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.