Class loading process

Source: Internet
Author: User
Tags reflection

The load flow of the class file is shown in the following diagram, where the order of loading, validation, preparation, initialization, and uninstallation is fixed, and parsing is likely to occur after initialization.


Conditions for class loading
Class is loaded only when it is used, and a class or interface must be initialized the first time it is actively used. Active use includes the following scenarios:
When creating an instance of a class, such as using the new keyword, or by reflection, cloning, deserialization;
When the static method of the class is called, the invokestatic instruction is used;
When using static fields of classes or interfaces (non-final constants), such as using getstatic or putstatic directives;
When using the method in the Java.lang.reflect package, the method of reflection class;
When initializing a subclass, first initialize the parent class;
The entry of the main function (main () function);
Here is an example of a passive reference:

/**
 * Passive use class field Demo: Referencing a static field of a parent class through a subclass does not cause subclasses to initialize
 * 
 * @author Xuefeihu * */Public
class Superclass {

	static {
		System.out.println ("Superclass init!");
	}
	
	public static int value = 123;
}

public class subclass extends superclass {
	
	static {
		System.out.println ("Subclass init!");
	}

}

public class Notinitialization {public

	static void Main (string[] args) {
		System.out.println (subclass.value) ;
	}
}

The result after the run is:

Superclass init!
123

Here is an example of a passive reference:

Loading the child will first load the parent public
class Parent {
	static {
		System.out.println ("Parent init");
	}
}

public class Child extends Parent {
	static {
		System.out.println (' child init ');
	}
}

public class Initmain {public
	static void Main (string[] args) {child
		c = new Child ();
	}
}

The results of the implementation are as follows:

Parent init child
Init

Load
The following three things are done during the class loading phase: ① The binary byte stream of this class through the fully qualified name of this class. ② converts the static storage structure represented by this byte stream into the runtime data structure of the method area. The ③ generates the corresponding Java.lang.Class object in the Java heap. Java bytecode can be obtained in the following ways:
(1) from the zip package (e.g. jar, ear, war, etc.)
(2) Get from the network
(3) Dynamic generation of the runtime, the most used is the dynamic agent (such as: JDK dynamic agent, cglib dynamic agent)
(4) Other file generation (e.g. JSP compilation generates the corresponding class file)
(5) Read from the database (relatively few)

Verify
The purpose of the verification is to ensure that the bytecode file is correct. The main methods of verification are as follows: Validation of ① file format, ② metadata validation, ③ bytecode verification (very complex), ④ symbol Reference validation
(1) Verification of file format
① whether to start with 0XCAFEBABY
② whether the primary and secondary version number is within the current JVM processing range
③ constant pool for unsupported constants
......
(2) meta-data validation
① whether there is a parent class
② inherits the final class.
③ non-abstract class implements all the abstract methods
......
(3) Byte code verification
① Run Check
② stack data type and opcode data parameters match
③ Jump instruction specified to a reasonable position
......
(4) Symbol reference verification
① the permission named in the symbol reference by string to find the corresponding class
Whether there is a field description in the ② class that conforms to the method and the method and field described by the simple name
③ whether the accessibility of classes, fields, and methods in a symbol reference can be accessed by the current class
......

Get ready
The preparation stage is to assign an initial value to a variable of type static, such as an initial value of 0 for an int type, and be aware that when the variable type is static final, the preparation stage assigns it to the final value.

Analytical
The JVM converts symbolic references (which are not related to the JVM) within a constant pool to direct references (which are relevant to the JVM). Parsing actions are primarily for classes or interfaces, fields, class methods, interface methods, method types, method handles, and call Point qualifier 7 class symbol references.

Initialization
The initialization phase includes the execution constructor, which includes the static variable assignment statement and the static{} block, which guarantees that the parent class is called before the call of the subclass, and that it is thread-safe.


Reference: "In-depth understanding of Java virtual machines"



Links: http://moguhu.com/article/detail?articleId=50

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.