Copy codeThe Code is as follows: class Parent {
// Static variables
Public static String p_StaticField = "parent class -- static variable ";
// Variable (in fact, this object can better be reflected in this point, such as writing a class instance)
// If this variable is placed behind the initialization block, an error is reported because it is not initialized at all.
Public String p_Field = "parent class -- variable ";
// Static initialization Block
Static {
System. out. println (p_StaticField );
System. out. println ("parent class -- Static initialization block ");
}
// Initialize the block
{
System. out. println (p_Field );
System. out. println ("parent class -- initialization block ");
}
// Constructor
Public Parent (){
System. out. println ("parent class -- constructor ");
}
}
Public class SubClass extends Parent {
// Static variables
Public static String s_StaticField = "subclass -- static variable ";
// Variable
Public String s_Field = "subclass -- variable ";
// Static initialization Block
Static {
System. out. println (s_StaticField );
System. out. println ("subclass -- Static initialization block ");
}
// Initialize the block
{
System. out. println (s_Field );
System. out. println ("subclass -- initialization block ");
}
// Constructor
Public SubClass (){
// Super ();
System. out. println ("subclass -- constructor ");
}
// Program entry
Public static void main (String [] args ){
System. out. println ("************** in main ***************");
New SubClass ();
System. out. println ("************** second subClass ***************");
New SubClass ();
}
}
Output result
Parent class-static variables
Parent class-static initialization Block
Subclass-static variable
Subclass-static initialization Block
* *********** In main ***************
Parent class -- Variable
Parent class -- initialize the block
Parent class -- Constructor
Subclass -- Variable
Subclass -- initialize the block
Subclass -- Constructor
* *********** Second subClass ***************
Parent class -- Variable
Parent class -- initialize the block
Parent class -- Constructor
Subclass -- Variable
Subclass -- initialize the block
Subclass -- Constructor
Result Analysis:
Obviously, after loading the main method, static variables are executed no matter whether the parent class or subclass, and then the common variables and constructors of the parent class and subclass. This is because when we want to create a subclass object, we find that this class requires a parent class, so we put the parent class. class is loaded in, and then its common variables and code block are initialized in sequence. Finally, its constructor can start the work of sub-classes and put the sub-classes. the class is loaded in and is working on the subclass.
In addition, the default constructor of the parent class is called by default in the subclass of Java, that is, super ().
Java has done it for you. We can do an experiment. If you comment out the default constructor in the parent class and add a constructor with parameters, if
Super (argument) is not added to the subclass. A syntax error is reported.
If we comment out all the content in the Main method, you will find that only
Parent class-static variables
Parent class-static initialization Block
Subclass-static variable
Subclass-static initialization Block
Others will not be output. Why? Also Research