loading, connecting, and initializing classes in Java

Source: Internet
Author: User

loading, connecting, and initializing classes in Java

Class loading, joining, and initializing

Let's introduce the JVM and class

JVM and class:

When we call the Java command to run a Java program, the command launches a Java Virtual machine process, no matter how complex the Java program is, all the threads that the program starts, all the variables are in the Java Virtual machine process, and they all use the memory area of the JVM process. The JVM process is terminated when the system has the following conditions.

1. The program runs to the last normal end;

2. The program runs to the end of the program using System.exit () or Runtime.getruntime (). exit () code;

3. An uncaught exception or error has been encountered during the program's operation;

4. The platform on which the program is located forces an end to the JVM process.

From the above, when the program runs at the end, the JVM process ends and the state in memory will be lost. The following example:

Defines a class containing static field a

public class A

{

public static int a = 6;

}

Define Class B to create an instance of Class A

public class B

{

A a1 = new A ();

a1.a++;

System.out.println (a1.a); Output 7

}

Define Class B to create an instance of Class A

public class C

{

A a2 = new A ();

System.out.println (a2.a); Output 6

}

The reason for output 6 in class C is that B and C are two runs of the JVM process, all changes to a after B is run are lost, and Class A is initialized the second time the JVM is run.

Classes are loaded:

The system may load the class the first time it is used, or it may use a preload mechanism to load a class.

When the program actively uses a class, if the class has not been loaded into memory, the system will be loaded, connected, initialized 3 steps to initialize the class, if there is no accident, the JVM will connect to complete the 3 steps, and all the 3 steps are sometimes referred to as class loading or class initialization.

Class loading refers to reading class files into memory and creating a Java.lang.Class object for them, that is, when a program uses any class, the system creates a Java.lang.Class object for it.

(Each class is an abstraction of a group of objects with the same characteristics, and all of the classes in the system are actually instances, both of which are instances of Java.lang.Class).

Classes are loaded by the ClassLoader, which is typically provided by the JVM, which is also the basis for all programs to run, and these classloader are often referred to as the system ClassLoader, and in addition to this, we can also create our own class loader by inheriting the ClassLoader base class. By using different class loaders, you can load the binary data of a class from different sources, through the following sources.

1. Load the class file from the local file system;

2. Load the class file from the jar package;

3. Load the class file via the network;

4. Dynamically compile a class source file and execute the load.

Connection to the class:

When the class is loaded, the system generates a corresponding class object, which is then entered into the connection phase, which is responsible for merging the class's binary data into the JRE, and the class connection can be divided into 3 phases:

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

2. Preparation: The class preparation phase is responsible for allocating memory for the static field of the class and setting the default initial value;

3. Resolution: Replace the symbolic reference in the binary data of the class with a direct reference.

Initialization of the class:

During the initialization phase of the class, the virtual machine is responsible for initializing the class, primarily to initialize the static field, and there are two ways to specify the initial value for the static Fieldr in the Java class: 1. Specifies the initial value when declaring a static field; 2. Use static initialization blocks to specify the initial values for static field. Such as:

public class Test

{

static int a = 5;

static int b;

static int C;

Static

{

b = 6;

}

}//Final Result a=5,b=6,c=0

Static initialization blocks are treated as initialization statements for classes, and the JVM executes them sequentially in the order in which they are arranged in the program, such as:

public class Test

{

Static

{

b = 6;

}

static int a = 5;

static int b = 9;

}//Final Result a=5,b=9

The JVM Initializes a class that consists of the following steps:

1. If the class has not been loaded and connected, the program loads and connects the class first;

2. If the immediate parent class of the class has not yet been initialized, initialize its immediate parent class (the direct parent class also performs the first-in-a-time guarantee that all parent classes dependent on the class will be initialized);

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

Class initialization time:

1. Create an instance of the class, including using the new operator, created by reflection, created by deserialization;

2. Call a static method of a class;

3. Access static field for a class or interface, or assign a value to the static field;

4. Force the creation of a Java.lang.Class object for a class or interface by means of reflection, for example, Class.forName ("person"), which would cause the person class to be initialized if the person class was not initialized. and returns the Java.lang.Class object corresponding to the person class;

5. Initialize a subclass of a class;

6. Use the Java.exe command directly to run a main class.

In addition: For a field-type static field, if the value of the field can be determined at compile time, then this field is equivalent to "macro variable", the Java compiler will be at compile time directly to the place where the field appears to replace its value, Because even if the program uses this static field, it does not cause initialization of the class.

loading, connecting, and initializing classes in Java

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.