Introduction to Simple Java Virtual machine class loading mechanism

Source: Internet
Author: User

Class starts from being loaded into the virtual machine memory, and its entire life cycle includes: Load (Loading)-->> validation (verification)-->> Preparation (preparation)-->>, until the memory is unloaded Parsing (Resolution)-->> initialization (initialization)-->> uses (using)-->> offload (unloading) 7 stages.

1. Loading

1) The binary byte stream of this class is obtained first through the fully qualified name of a class.

2) Secondly, the static storage structure represented by this byte stream is transformed into the runtime data structure of the method area.

3) Finally, a class object is generated in the Java heap that represents it as the access entry for the data in the method area.

2. Verification

The purpose of the verification is to ensure that the information contained in the byte stream of the class file conforms to the requirements of the current virtual machine and does not compromise the security of the virtual machine itself.

3. Preparation

1) Allocate memory for class variables and set the class variable initial value (static modified variable).

2) The constant value is initialized to the specified value in the code, but does not include the static final instance variable, and the static final decorated instance variable is assigned value when the class is initialized. The initial value of the class's very static variable is the default null value.

4. Parsing

To convert a symbol reference in a class to a direct reference

5. Initialization

(1) Trigger conditions for initialization:

1) Access static variables of the class or interface, or assign values to static variables of the class.

2) Create an instance of the class, that is, the new object

3) Calling the static method of the class

4) Reflection (Class.forName ("JAVA.LANG.OBJECTT)")

5) Subclass is initialized

6) Specify a class as the initialization class when the Java Virtual machine starts (the startup class that contains the Main method)

(2) Order of initialization

1) static variable (non-static final variable.) Included) is accessed or a static method is called: the code block of the parent class-->> the static variable of the parent class-->> the code block of the child class-->> the static variable of the child class-->> the static method of the subclass

2) Subclasses are instantiated: the code block of the parent class-->> the static variable of the parent class-->> the code block of the child class-->> the static variable of the subclass-->> the parent class construction method (default is super ())-->> Sub-class construction methods

3) Static variables are initialized only once.

Write to your own words: With regard to the construction method, when the constructor method has this () or super (), it will always look for a constructor call that is not displayed before calling an implicit constructor.

  

Introduction to Simple Java Virtual machine class loading mechanism

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.