01. Class Loader

Source: Internet
Author: User

Why study the whole process of class loading?

---Helps to understand the JVM's running process

---more in-depth understanding of Java Dynamics (antipyretic deployment, dynamic loading) for improved program flexibility

---The most important thing is that it facilitates the analysis of various web containers, the principle of Android plugin

Class loading mechanism

The JVM loads the class file into memory and verifies, parses, and initializes the data, eventually creating a Java type that the JVM can use directly

Copy the picture as follows:

Class loading whole process

1. Loading:

Load the bytecode into memory and convert the data into a data structure running in the method area, generate a Java.lang.Class object representing the class in the heap as the access entry for the data in the method area, and of course the process requires the participation of the classloader of the class

2. Links:

The process of merging the Java class's binary code into the running state of the JVM, during

1) Verify:

--Ensure that the information in the load class conforms to the JVM specification, without security issues

2) Prepare:

--a phase that formally allocates memory for a class variable (static variable) and sets the initial value of the class variable, which are allocated in the method area

Note: The initial value of the class variable is assigned at the preparation stage, such as the static int a= 10, then its original values are 0, and the value of a is changed to 10 only at initialization time;

3) Analysis:

--the process of replacing a symbolic reference within a constant pool in a virtual machine with a direct reference

Note string constant values: General class names, method names, parameter names, variable names are constants.

3. Initialization

1) The initialization phase is the process of executing the class constructor <clinit> () method, and the class constructor <clinit> () method is generated by the compiler automatically collects assignment actions for all class variables in the class and statement merges in static statement blocks (static blocks).

Class constructors are not created by us.

2) When you first start a class, if you find that the parent class has not been initialized, you need to trigger the initialization of its parent class first

3) Virtual opportunity ensures that the <clinit> () method of a class is correctly locking and synchronized in a multithreaded environment.

4) When accessing a static domain of a Java class, only the classes that actually declare the domain are initialized.

A picture is needed here

Active and passive references to classes

1. Active and passive references to classes (initialization of classes must occur)

L Objects of the new class

L call static members of a class (except for final constants) and static methods

L Use the Java.lang.reflect package method to reflect and invoke a class

When the virtual machine starts, the Java demo will initialize the demo class, starting with the class where the main method resides.

When a class is initialized, its parent class is initialized first if its parent class is not initialized

2. A passive reference to a class does not occur initialization

Defining a class reference through an array does not trigger initialization of this class:

A a[] = new a[]; this does not trigger the initialization of Class A.

When accessing a static domain, only the classes that actually declare the domain are initialized:

The parent class initializes when the child class accesses the static domain declared by the parent class, but the subclass is not initialized.

01. Class Loader

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.