Connected load and initialize Java classes

One o'clock eye

Load class 1

When the program is actively using a class if the class has not been loaded into memory, by loading the system, connected to the initialization step to initialize the three classes, if not unexpected, the JVM will complete three consecutive step, so sometimes these three steps collectively referred to as class loading or class initialization.

Class load refers to the class of the class file is read into memory, and whom to create a java.lang.Class objects, that is when a program uses any class, whom the system will establish a java.lang.Class object.

Class 2 data source

By using a different class loader, the binary data from different sources can be loaded classes are usually several sources:

  • To load class files from the local file system, which is the vast majority of class loading.

  • Loaded from the package JAR file class, this approach is very common, database-driven class is used when the ODBC programming on the JAR file, JVM class files that can be loaded directly from the JAR file.

  • Load class files over the network.

  • Put a dynamically compiled Java source file, and perform the load.

Class 3 is connected

When after the class is loaded, the system generates whom a corresponding Class object, and then will enter the connection phase, the connection will be responsible for the combined stage binary data to the JRE class. Type connector can be divided into the following three stages:

Verification: verification phase used to test whether the loaded class has the correct internal structure and coherence, and other classes.

Preparation: class preparation phase is responsible for the static properties of the class allocates memory, and set the default initial value.

Analysis: The binary data class replaced symbolic references direct reference.

4 class initialization

In the class initialization phase, the virtual machine is responsible for the class to initialize the main thing is to initialize static properties. In the Java class There are two ways to specify the initial value of the static properties:

  • Specifies the initial value when you declare a static property.

  • Using static initialization block is assigned an initial value for the static properties.

Step 5 JVM class initialization

5.1 If the class has not been loaded and connected, and connected to a program loads the class.

5.2 If the direct parent class has not been initialized, it first initializes its direct parent.

5.3 If the class initialization statements, the system sequentially performs the initialization statement.

Two combat

Code 1

public class Test
{
   static
   {
      // 使用静态初始化块为变量b指定出初始值
      b = 6;
      System.out.println("----------");
   }
   // 声明变量a时指定初始值
   static int a = 5;
   static int b = 9;         // ①
   static int c;
   public static void main(String[] args)
   {
      System.out.println(Test.b);
   }
}

2 runs

----------
9

 

 

Guess you like

Origin blog.csdn.net/chengqiuming/article/details/94957506