Copy Code code as follows:
public class Test4 {
@Test
public void Test () {
Child child = new Child ();
}
}
Class parent{
public static String Parentstaticfield = "Parent class static variable";
public String Parentnormalfield = "Parent class common variable";
Static {
System.out.println ( Parentstaticfield);
System.out.println ("Parent class static Block");
}
{
System.out.println (Parentnormalfield) ;
System.out.println ("normal block of parent");
}
Public parent () {
System.out.println ( "Parent class construction method");
}
}
Class Child extends parent{
public static String Childstaticfield = "Subclass static variable";
Public String Childnormalfield = "Subclass generic variable";
static {
System.out.println (Childstaticfield);
System.out.println ("Subclass static Block");
}
{
System.out.println (Childnormalfield);
System.out.println ("Subclass common Block");
}
Public Child () {
SYSTEM.OUT.PRINTLN ("Subclass construction Method");
}
}
Output:
Copy Code code as follows:
Parent class static variable
Parent class static Block
Subclass Static Variables
Child class static Block
Parent class Generic Variable
Normal block of parent class
Parent class Construction Method
Subclass Generic Variable
Subclass Common Block
Sub-Class construction method
Execution process:
1. When executing to the new child, the loader looks for the code of the child class that has been compiled (that is, the Child.class file). In the process of loading, the loader notices that it has a base class, and then it loads the base class. Whether you create a base class object or not, this process always happens. If the base class also has a base class, the second base class is also loaded, and so on.
2. Perform the static initialization of the base class, then the static initialization of the next derived class, and so on. This order is important because the "static initialization" of a derived class may depend on the correct initialization of the base class member.
3. Create a child class object when all necessary classes have been loaded.
4, the child class exists the parent class, the constructor of the parent class is invoked, and super can be used to specify which constructor to invoke.
The base class is constructed and constructed in the same order as the derived class. First, the variables in the base class are initialized in literal order, and then the rest of the constructor of the base class is executed.
5. The child class member data is initialized in the order in which they are declared, and the remainder of the subclass constructor is executed.