The class loading process in Java is fully resolved

  • 2020-11-20 06:04:55
  • OfStack

The order in which class files are loaded

1. First load the static variables and static initializer blocks of the parent class (in the order of execution)

2. Reload the static variables and static initialization blocks that execute this class

As long as the class is not destroyed, static variables and static initializers are executed only once, and subsequent operations on the class do not perform these two steps.

Class instance creation process

An instance of the class is created only when the new method is called

1. Follow the order in which the class files are loaded (skip this step if the class has been loaded)

2. Non-static variables and non-static initialization blocks of the parent class

3. Constructor of the parent class

4. Non-static variables and non-static initialization blocks of this class

5. Constructor of this class

4, class instance destruction, first destroy the subclass part, then destroy the parent part

Both static and non-static methods are called passively

That is, the system does not automatically invoke execution. The main difference is that static methods can be called directly with the class name (as can instantiated objects), whereas non-static methods can only be called after instantiating the object.

Relevant concepts

Key words: static

Is a modifier used to modify members (member variables and member functions)

The modified member has the following characteristics:

Load as the class loads (when class 1 loads, static data immediately loads space in memory)

When a class disappears, it has the longest life cycle

Takes precedence over object existence (object disappears, static is still there)

Static exists first, objects exist later

Shared by all objects

Save memory space

When a member is statically decorated, it can be called directly by the class name in addition to the object

Class name. Static member

Use attention

Static methods can only access static members (methods and variables)

Non-static methods can access both static and non-static methods

You cannot write the this, super keyword in a static method

Because static takes precedence over object existence, this cannot appear in static methods

The main function is static

publicstaticvoidmain(String[]args){}

When to use static?

You do this in two ways: because static modifiers include member variables and functions.

When to define static variables (class variables)

When Shared data appears in an object, the data is statically decorated. The specific data in the object is defined to exist in heap memory as non-static.

When to define a static function

A function can be defined as static when non-static data (object-specific data) is not accessed internally.

Static pros and cons

Leon:

1. Store the Shared data of the object in a separate space to save space. It is not necessary to store 1 copy per object.

2, can be directly called by the class name

Disadvantages:

1. Too long life cycle

2. Limited access (only static access)

Memory structure

The Java program needs to allocate space in memory while it is running. In order to improve the efficiency of computing, the space is divided into different areas, because each area has a specific way of processing data and memory management.

Stack memory

Used to store local variables. When the data is used up, the space occupied will be released automatically

Heap memory

Arrays and objects (entities), established by new instances are stored in a heap memory (member variables with the establishment of the object, is present in the object's heap memory) every one entity has a memory address value (variable by address references) variables with the entity has the default initialization value entity will no longer be used, will be in the uncertain time reclaimed by the garbage collector (gc)

Method area, local method area, register

validation

加载顺序 父类静态变量=1 父类非静态变量=1 子类静态变量=1 子类非静态变量=1
【父类调用父类静态方法】 Parent.pStaticMethod();      
父类静态初始化块1 2      
父类静态初始化块2 3      
父类静态方法 4      
【子类调用子类静态方法】 Child.cStaticMethod();      
子类静态初始化块1 5   2  
子类静态初始化块2 6   3  
子类静态方法 7   4  
【子类实例化】 Child c=new Child();      
父类非静态初始化块1 8 2    
父类非静态初始化块2 9 3    
父类构造方法 10 4    
子类非静态初始化块1 11 5 5 2
子类非静态初始化块2 12 6 6 3
子类构造方法 13 7 7 4
【父类实例化子类对象】 Parent p=new Child();      
父类非静态初始化块1 14 2    
父类非静态初始化块2 15 3    
父类构造方法 16 4    
子类非静态初始化块1 17 5 8 2
子类非静态初始化块2 18 6 9 3
子类构造方法 19 7 10 4
加载顺序 父类静态变量=1 父类非静态变量=1 子类静态变量=1 子类非静态变量=1
【子类实例化】 Child c=new Child();      
父类静态初始化块1 2      
父类静态初始化块2 3      
子类静态初始化块1 4   2  
子类静态初始化块2 5   3  
父类非静态初始化块1 6 2    
父类非静态初始化块2 7 3    
父类构造方法 8 4    
子类非静态初始化块1 9 5 4 2
子类非静态初始化块2 10 6 5 3
子类构造方法 11 7 6 4
【父类实例化子类对象】 Parent p=new Child();      
父类非静态初始化块1 12 2    
父类非静态初始化块2 13 3    
父类构造方法 14 4    
子类非静态初始化块1 15 5 7 2
子类非静态初始化块2 16 6 8 3
子类构造方法 17 7 9 4
【父类调用父类静态方法】 Parent.pStaticMethod();      
父类静态方法 18      
【子类调用子类静态方法】 Child.cStaticMethod();      
子类静态方法 19   10  


public class ClassTest {  
  public static void main (String args[]) {
    System.out.println(" [Subclass instantiation] |Child c=new Child();");   
    Child c=new Child();
    System.out.println(" [Parent class Instantiation subclass object] |Parent p=new Child();");    
    Parent p=new Child();
    System.out.println(" [Superclass calls superclass static methods] |Parent.pStaticMethod();");
    Parent.pStaticMethod();
    System.out.println(" [Subclass calls subclass static method] |Child.cStaticMethod();");
    Child.cStaticMethod();
  }
}

public class ClassTest2 {  
  public static void main (String args[]) {
    System.out.println(" [Superclass calls superclass static methods] |Parent.pStaticMethod();");
    Parent.pStaticMethod();
    System.out.println(" [Subclass calls subclass static method] |Child.cStaticMethod();");
    Child.cStaticMethod();
    System.out.println(" [Subclass instantiation] |Child c=new Child();");   
    Child c=new Child();
    System.out.println(" [Parent class Instantiation subclass object] |Parent p=new Child();");    
    Parent p=new Child();
  }
}

public class Parent {
  //  Parent static variables 
  static int m = 1;  
  //  Parent non-static variables 
  int n = 1;
 
  //  Static statement block 1
  static {
    m++;
    // j++;  A parent non-static variable cannot be used in a static statement block 
    System.out.println(" The parent class initializes the block statically 1|" + m);
  }
  //  Static statement block 2
  static {
    m++;
    System.out.println(" The parent class initializes the block statically 2|" + m);
  }
 
  //  The constructor 
  public Parent() {
    m++;
    n++;
    System.out.println(" Superclass constructors |" + m + "|" + n);
  }
 
  //  Non-static statement block 
  {
    m++;
    n++;
    System.out.println(" The parent class non-statically initializes the block 1|" + m + "|" + n);
  }
 
  //  Non-static statement block 
  {
    m++;
    n++;
    System.out.println(" The parent class non-statically initializes the block 2|" + m + "|" + n);
  }
 
  //  Nonstatic method 
  public void pMethod() {
    m++;
    n++;
    System.out.println(" Parent non-static methods |" + m + "|" + n);
    return;
  }
 
  //  A static method 
  public static void pStaticMethod() {
    m++;
//   j++;  Parent class non-static variables cannot be used in static methods 
    System.out.println(" Parent static methods |" + m);
    return;
  }
 
  @Override
  protected void finalize() throws Throwable {
    super.finalize();
    System.out.println(" Destruction of the parent class |");
  }
}

public class Child extends Parent {
  //  A static variable 
  static int i = 1;
  //  Nonstatic variable 
  int j = 1;
 
  //  Static statement block 1
  static {
    m++;
    i++;
    // j++;  Non-static variables cannot be used in static statement blocks 
    System.out.println(" A subclass initializes the block statically 1 " + "|" + m + "||" + i);
  }
  //  Static statement block 2
  static {
    m++;
    i++;
    System.out.println(" A subclass initializes the block statically 2 " + "|" + m + "||" + i);
  }
 
  //  The constructor 
  public Child() {
    m++;
    n++;
    i++;
    j++;
    System.out.println(" Subclass constructor  " + "|" + m + "|" + n + "|" + i + "|" + j);
  }
 
  //  Non-static statement block 
  {
    m++;
    n++;
    i++;
    j++;
    System.out.println(" A subclass non-statically initializes a block 1" + "|" + m + "|" + n + "|" + i + "|" + j);
  }
  //  Non-static statement block 
  {
    m++;
    n++;
    i++;
    j++;
    System.out.println(" A subclass non-statically initializes a block 2" + "|" + m + "|" + n + "|" + i + "|" + j);
  }
 
  //  Nonstatic method 
  public void pMethod() {
    m++;
    n++;
    i++;
    j++;
    System.out.println(" Subclasses inherit non-static methods " + "|" + m + "|" + n + "|" + i + "|" + j);
    return;
  }
 
  //  A static method 
  public static void pStaticMethod() {//  Static methods cannot be inherited 
    m++;
    i++;
    // j++;  Non-static variables cannot be used in static methods 
    return;
  }
 
  //  Nonstatic method 
  public void cMethod() {
    m++;
    n++;
    i++;
    j++;
    System.out.println(" Subclass non-static methods " + "|" + m + "|" + n + "|" + i + "|" + j);
    return;
  }
 
  //  A static method 
  public static void cStaticMethod() {
    m++;
    i++;
    // j++;  Non-static variables cannot be used in static methods 
    System.out.println(" Subclass static method  " + "|" + m + "||" + i);
    return;
  }
 
  @Override
  protected void finalize() throws Throwable {
    super.finalize();
    System.out.println(" Destruction of the subclass |");
  }
}

conclusion

That's the end of this article on the Java classloading process, and I hope you found it helpful. If you have any questions, please feel free to leave a message. This site will reply to you in time. We look forward to your valuable comments.


Related articles: