Jvm class loading mechanism Depth - several stages classloading

We know the life cycle java class is loaded , connected , initialization , use and unloading five processes, as shown below:


1. Load 

    We write a java class code, compiled after generating a file name suffix of .class, java virtual machine will be able to recognize this file. java life cycle is a class file is loaded into the process from extinction. About loading, in fact, it is the source of information to find the class file in the class load it into the method area, then instantiate a java.lang.Class object heap, as the class information of this entry area methods. However, this feature is implemented outside the JVM, the main reason is to make it easier for applications to decide how to get the class, not necessarily in the same way different virtual machine implementation, hotspot virtual machine is required when using the loading mode , there are other is the first pre-loaded. (Refer to JVM-depth understanding of the book)

2. Connect the 
    connection is generally loaded phase and the initialization phase cross, the process consists of the following three parts:
    (1) Validation: to determine whether the class-compliant java language, there is no repeat of the properties and behavior, inheritance is reasonable, in short, It is to ensure jvm able to perform 
    (2) preparation: the main job is modified by the static member variables to allocate memory and set the default initial value of the 
        default initial values are as follows:

                    1. The eight basic data types default initial value is 0. 
                    2. The default initial reference type is null 

                    3. There static final modification will direct assignment, such as: static final int x = 10; the default is 10.

(3) Analysis: This phase of the mission is to sign the constant pool references converted to a direct reference, it means jvm will all class or interface names, field names, method names converted to a specific memory address.

3. The initialization 
    process is to initialize the phase static variables (class variables) assignment, i.e. can only be modified static initialization sequence is performed: the parent class static fields or static block of code, followed by a static field or sub-subclass class static code block (static block of code is loaded first, then the static properties)

4. Use 

    Still exist in the course of the following three steps class:

    (1) Object Instantiation: is the content execution class constructor, if there is a parent class of the class by the JVM displayed or implicit manner prior parent class constructor is executed, the parent class instance variables in the open heap memory space, and given a default initial value, and the code in accordance with the content of the constructor function value to the real instance variable itself, then the first address of the variable reference object acquired, instance variables and methods to call the operation target by 

    (2) Garbage collection: when the object is no longer referenced, the virtual machine will be particularly marked in the trash mark, waiting for GC heap recovery 
    end (3) of the object: the object is recovered GC, the object is no longer existence, the object of life will come to an end
5. uninstall class 
    lifecycle class that is unloading the class reached the final step, the program no longer has a reference to the class, the class will also be garbage collection JVM, from the end of life ...

Code Example:

package com.etc.test;
class  A{
     static  int  a; //类变量
     String name;
     int  id;
     //静态代码块
     static {
         a=10;
         System. out .println( "这是父类的静态代码块" +a);
     }
     //构造代码块
     {
         id=11;
         System. out .println( "这是父类的构造代码块id:" +id);
     }
     A(){
         System. out .println( "这是父类的无参构造函数" );
     }
     A(String name){
         System. out .println( "这是父类的name" +name);
     }
}
class  B extends A{
     String name;
     static  int  b;
     static {
         b=12;
         System. out .println( "这是子类的静态代码块" +b);
     }
      B(String name) {
         super();
         this .name = name;
         System. out .println( "这是子类的name:" +name);
     }
}
public  class  Test666 {
public  static  void  main(String[] args) {
     B bb= new  B( "GG" );
}
}

We know the life cycle java class is loaded , connected , initialization , use and unloading five processes, as shown below:


1. Load 

    We write a java class code, compiled after generating a file name suffix of .class, java virtual machine will be able to recognize this file. java life cycle is a class file is loaded into the process from extinction. About loading, in fact, it is the source of information to find the class file in the class load it into the method area, then instantiate a java.lang.Class object heap, as the class information of this entry area methods. However, this feature is implemented outside the JVM, the main reason is to make it easier for applications to decide how to get the class, not necessarily in the same way different virtual machine implementation, hotspot virtual machine is required when using the loading mode , there are other is the first pre-loaded. (Refer to JVM-depth understanding of the book)

2. Connect the 
    connection is generally loaded phase and the initialization phase cross, the process consists of the following three parts:
    (1) Validation: to determine whether the class-compliant java language, there is no repeat of the properties and behavior, inheritance is reasonable, in short, It is to ensure jvm able to perform 
    (2) preparation: the main job is modified by the static member variables to allocate memory and set the default initial value of the 
        default initial values are as follows:

                    1. The eight basic data types default initial value is 0. 
                    2. The default initial reference type is null 

                    3. There static final modification will direct assignment, such as: static final int x = 10; the default is 10.

(3) Analysis: This phase of the mission is to sign the constant pool references converted to a direct reference, it means jvm will all class or interface names, field names, method names converted to a specific memory address.

3. The initialization 
    process is to initialize the phase static variables (class variables) assignment, i.e. can only be modified static initialization sequence is performed: the parent class static fields or static block of code, followed by a static field or sub-subclass class static code block (static block of code is loaded first, then the static properties)

4. Use 

    Still exist in the course of the following three steps class:

    (1) Object Instantiation: is the content execution class constructor, if there is a parent class of the class by the JVM displayed or implicit manner prior parent class constructor is executed, the parent class instance variables in the open heap memory space, and given a default initial value, and the code in accordance with the content of the constructor function value to the real instance variable itself, then the first address of the variable reference object acquired, instance variables and methods to call the operation target by 

    (2) Garbage collection: when the object is no longer referenced, the virtual machine will be particularly marked in the trash mark, waiting for GC heap recovery 
    end (3) of the object: the object is recovered GC, the object is no longer existence, the object of life will come to an end
5. uninstall class 
    lifecycle class that is unloading the class reached the final step, the program no longer has a reference to the class, the class will also be garbage collection JVM, from the end of life ...

Code Example:

package com.etc.test;
class  A{
     static  int  a; //类变量
     String name;
     int  id;
     //静态代码块
     static {
         a=10;
         System. out .println( "这是父类的静态代码块" +a);
     }
     //构造代码块
     {
         id=11;
         System. out .println( "这是父类的构造代码块id:" +id);
     }
     A(){
         System. out .println( "这是父类的无参构造函数" );
     }
     A(String name){
         System. out .println( "这是父类的name" +name);
     }
}
class  B extends A{
     String name;
     static  int  b;
     static {
         b=12;
         System. out .println( "这是子类的静态代码块" +b);
     }
      B(String name) {
         super();
         this .name = name;
         System. out .println( "这是子类的name:" +name);
     }
}
public  class  Test666 {
public  static  void  main(String[] args) {
     B bb= new  B( "GG" );
}
}

Guess you like

Origin blog.csdn.net/qq_42239765/article/details/82558289