Discuss the loading order of parent and subclass in Java

  • 2020-04-01 02:00:23
  • OfStack


class Parent {
    //A static variable
    public static String p_StaticField = " The parent class -- A static variable ";
    //Variables (actually this is better with objects, such as writing an instance of a class)
  
    //If the variable is placed after the initialization block, an error will be reported, because you are not initialized at all
    public String p_Field = " The parent class -- variable ";
    //Static initialization block
    static {
        System.out.println(p_StaticField);
        System.out.println(" The parent class -- Static initialization block ");
    }
    //The initialization block
    {
        System.out.println(p_Field);
        System.out.println(" The parent class -- The initialization block ");
    }
    //The constructor
    public Parent() {
        System.out.println(" The parent class -- The constructor ");
    }
}
public class SubClass extends Parent {
    //A static variable
    public static String s_StaticField = " A subclass -- A static variable ";
    //variable
    public String s_Field = " A subclass -- variable ";
    //Static initialization block
    static {
        System.out.println(s_StaticField);
        System.out.println(" A subclass -- Static initialization block ");
    }
    //The initialization block
    {
        System.out.println(s_Field);
        System.out.println(" A subclass -- The initialization block ");
    }
    //The constructor
    public SubClass() {
        //super();
        System.out.println(" A subclass -- The constructor ");
    }
    //Program entrance
    public static void main(String[] args) {
        System.out.println("*************in main***************");
        new SubClass();
        System.out.println("*************second subClass***************");
        new SubClass();
    }
}

The output
Parent class - static variables
Parent class - static initialization block
Subclasses -- static variables
Subclass - static initialization block
* * * * * * * * * * * * * in the main * * * * * * * * * * * * * * *
Parent - variable
Parent class - initializes the block
Parent - constructor
Subclasses -- variables
Subclass -- initializes the block
Subclass -- constructor
* * * * * * * * * * * * * second ttf_subclass * * * * * * * * * * * * * * *
Parent - variable
Parent class - initializes the block
Parent - constructor
Subclasses -- variables
Subclass -- initializes the block
Subclass -- constructor

Result analysis:
Obviously, after the main method is loaded, the static variables are executed in both parent and subclass, followed by the normal variables and constructors in both parent and subclass. This is because, when you subclass this object, you find that the class needs a parent class, so you load in the parent's.class, and then you initialize its normal variables and then the initialization block, and then the constructor, and then you can start the subclass's work, and then you load in the subclass's.class, and then you do the subclass's work.

In addition, in Java subclasses there is a default constructor called super() that calls the parent class when there is only a default constructor
Java does it for you, and we can do an experiment if we comment out the default constructor in the parent class and add a constructor with arguments if
We don't have super(argument) in our subclass, so we're going to have a syntax error
If we comment out everything in the Main method, you'll see that it only prints

Parent class - static variables
Parent class - static initialization block
Subclasses -- static variables
              Subclass - static initialization block
              I'm not going to output anything else. The reason? Will study

Related articles: