Detail the loading order of the classes in Java

  • 2020-05-30 20:03:45
  • OfStack

This article describes the loading order of classes in Java. Let's take a look at the details below:

1. When the virtual machine loads Java class for the first time, it initializes the static initialization block, static member variable and static method once

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

3. Class instance creation process: initialization is carried out according to the parent-child inheritance relationship. Firstly, the initialization block of the parent class is executed, followed by the constructor of the parent class; Then execute the initialization block of the subclass that this class inherits, and finally the subclass constructor

4. When destroying a class instance, first destroy the subclass part, and then destroy the parent part

The sample


public class Parent
{
 public static int t = parentStaticMethod2();
 {
 System.out.println(" The parent class initializes the block nonstatically ");
 }
 static
 {
 System.out.println(" The parent class statically initializes the block ");
 }
 public Parent()
 {
 System.out.println(" The constructor of the parent class ");
 }
 public static int parentStaticMethod()
 {
 System.out.println(" The static method of the parent class ");
 return 10;
 }
 public static int parentStaticMethod2()
 {
 System.out.println(" Static methods of the parent class 2");
 return 9;
 }
 
 @Override
 protected void finalize() throws Throwable
 {
 // TODO Auto-generated method stub
 super.finalize();
 System.out.println(" Destruction of the parent class ");
 }
 
}

public class Child extends Parent
{
 {
 System.out.println(" Subclass non-static initializer block ");
 }
 static
 {
 System.out.println(" The subclass statically initializes the block ");
 }
 public Child()
 {
 System.out.println(" The constructor of a subclass ");
 }
 public static int childStaticMethod()
 {
 System.out.println(" Static methods for subclasses ");
 return 1000;
 }
 @Override
 protected void finalize() throws Throwable
 {
 // TODO Auto-generated method stub
 super.finalize();
 System.out.println(" Destruction of the subclass ");
 }
}

public class Test
{
 
 public static void main(String[] args)
 {
 // TODO Auto-generated method stub
 Parent.parentStaticMethod();
// Child child = new Child();
 
 }

}

The output


 Static methods of the parent class 2
 The parent class statically initializes the block 
 The static method of the parent class 

The static method in the class is loaded on the first call, and the static members in the class are loaded in the order they appear in the class. Output when calling static method 2


 Static methods of the parent class 2
 The parent class statically initializes the block 
 Static methods of the parent class 2

Comment out the Parent.parentStaticMethod();

Remove the comment Child child = new Child();


 Static methods of the parent class 2
 The parent class statically initializes the block 
 The subclass statically initializes the block 
 The parent class initializes the block nonstatically 
 The constructor of the parent class 
 Subclass non-static initializer block 
 The constructor of a subclass 

conclusion

The above is the whole content of this article, I hope the content of this article to your study or work can bring 1 definite help, if you have questions you can leave a message to communicate.


Related articles: