Java class loading process

Source: Internet
Author: User

A. class file defines a type in the JVM, including fields, methods, inheritance information, annotations, and other metadata. The format of the class file is described in detail in the specification, and any language that you want to run on the JVM must be followed.

  

The purpose of the JVM is to use the class file and execute its bytecode. To do this, the JVM must extract the contents of the class file in a byte stream and convert it into a usable format to join the running state. This two-step process is called loading and linking, but the connection is broken down into several sub-stages.

Load

This process first reads the byte data stream that makes up the class file and thaws the class's representation. The process begins by creating a byte array whose contents are usually read from the file system and then producing the class object corresponding to the loaded classes. There are some basic checks on the class during this process, but the class object is not mature at the end of the loading process, so classes are not available.

Connection

After the load is complete, the class must be connected, and this step is divided into three sub-stages to validate, prepare, parse. The validation phase confirms that the class file meets expectations and does not cause system run-time errors or other problems. This is followed by the preparation phase of the class, where all other types referenced in the class file are positioned to ensure that the class is ready. The relationships between the sub-stages in the join step are as follows

Verify

Validation is a very complex process, it is divided into several steps, the first is the integrity check, which is actually part of the loading process, will ensure that the class file is well-formed, you can connect and then check the constant pool of symbolic information is consistent, and to abide by the basic code of conduct of constants. Other static checks that do not involve code are also done at this stage, such as checking that the final method has not been rewritten. After that is the most complex part of the verification method, the bytecode check to check bytecode behavior is good, and does not attempt to get rid of the VM environment control, here are some major checks

    • Whether all methods adhere to the restrictions of access control keywords
    • The number of arguments and the static type of the method call are correct
    • Make sure bytecode does not attempt to misuse the stack
    • Ensure that the variable is initialized correctly before it is used
    • Checks whether a variable is only given the appropriate type of value

Get ready

The preparation of the class includes allocating the class and preparing the static variable in the initialization class, but not initializing the variable now, nor executing any VN bytecode

Analytical

Parsing initializes the VM to check whether the type referenced in the class file is a known type. If there are types of run-time locations, they also need to be loaded in. An unknown type that is visible in this row will again cause the class loading process to occur. Once the other types that need to be loaded are fully positioned and resolved, the VM can initialize the class that was originally loaded. All static variables can be initialized at this time, and all static initialization code blocks will run. Now the bytecode you are running is from the newly loaded class. Once this step is complete, all classes are complete and the class is ready to use.

Java class loading process

Related Article

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.