Three types of initialization overview

Source: Internet
Author: User

--lofty high-rise ground

1. Class loading, linking, initialization

Class 1.1 Loading: Reads class file into memory and creates a Java.lang.Class object for it

Connection to class 1.2:

Validation: The validation phase is used to verify that the class being loaded has the correct internal structure and is consistent with other classes

Prepare: The class preparation phase allocates memory for class variables of classes and sets default initial values

Parsing: Replacing symbolic references in binary data of a class with direct drinking

1.3 Initialization: Class variables are primarily initialized

Cases:

 Public class Test {    static{        a=1;    }      Public Static int a=5;      Public Static void Main (string[] args) {        System.out.println (TEST.A);    }}

For the above example code parsing: first compile the Test.java file into a Test.class file (compiled with the Javac command). When running (Java command), start the previous step (use the class for the first time or preload the class).

Class loading: Generate Test.class file to read into memory and generate corresponding class object, class object can be obtained by class<test> c= Test.class method;

Class Connection: The prepare phase allocates memory for class variables and assigns initial values. That is, the 4-byte space is used for variable A, and the initial value is a=0. (about the default value can query the API or find Niang, like in JavaScript declaration in advance, that will always put public static int a=0 in front, and then execute a=1;a=5;)

Initialization: The initialization order is from top to bottom, that is, execute a=1 first, then execute a=5; so the output test.a=5

2. Class initialization timing: When a class is initialized

2.1 Create the class instance. For example, creating an object with new or creating an object from reflection (creating an object not only instantiates the class, but also the object is instantiated).

2.2 Call a static method of a class or manipulate a static property (static-decorated). For example, in the previous example, call Test.a in the Main method

2.3 Use reflection to create a class of classes, such as the previous example:class<test> c= test.class or class<test> c = class.forname ("Test"), when the object is not initialized, Just class initialization.

2.4 When a class is initialized, its parent class is initialized first.

2.5 When you run the main (main Class) method.

3. Class load Source: Through different ClassLoader

3.1 Load the class file from the local file system. For example, a program written in the IDE (such as Eclipse) is automatically compiled into a class file, which is the class file that is loaded when you click Run.

3.2 Load the class file from the jar package. For example, a database-driven class jar package used by JDBC programming, or a lot of jar packages that need to be downloaded using the spring Framework to write programs

3.3 Load the class file over the network.

3.4 Dynamically compiles a Java source program and executes the load.

4. The JVM Initializes a few steps of a class

4.1 If this class has not yet been loaded and connected, the program loads and connects the class first

4.2 If the immediate parent class of the class has not been initialized, the immediate parent class is initialized first

4.3 If there are initialization statements in the class, the system executes the initialization statements sequentially

The 3.1~3.3 step is repeated when the parent class is initialized to 3.2, so the JVM is always initialized first with the Java.lang.Object class.

5. End of Java program

5.1 program runs to last normal end

5.2 The program runs to use System.exit () or Runtime.getruntime (). exit () code

5.3 End of an uncaught exception or error encountered in program execution

5.4 The platform on which the program is located forces the end of the JVM process. For example: Open Task Manager ctrl+shift+esc (WINDOW7) to find the process forced to end

Time: January 13, 2016 09:56:05

Note: The content is mainly excerpted from the "Crazy Java handout" with Mr. Li.

Three types of initialization overview

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.