We often use inheritance in projects, but we often don't quite understand the sequence and principle of program running, especially when the transformation object is used and the parent class subclass contains static variables and methods, I don't know who runs it first. I also wrote an example. Summary.
Copy codeThe Code is as follows:
Parent class:
Public class TestStatic {
Public static String name = "china ";
{
System. out. println ("======= method body ======= ");
}
Static {
Name = "England ";
System. out. println ("======= static program block ===== ");
}
TestStatic (){
System. out. println ("========= constructor ======= ");
}
Public static void main (String [] args ){
System. out. println ("======= main method =======" + name );
}
Public void test (){
System. out. println ("========= test method ========= ");
}
}
Subclass:
Public class TestExtendStatic extends TestStatic {
// Public static String name = "HUBEI ";
{
System. out. println ("======= no name method body ======= ");
}
Static {
// Name = "SUIZHOU ";
System. out. println ("======= subclass static program block ===== ");
}
TestExtendStatic (){
System. out. println ("========= subclass construction method ======= ");
}
Public void test (){
System. out. println ("========= subclass test method ========= ");
}
Public static void main (String [] args ){
System. out. println ("========= subclass main method ========" + name );
TestStatic ts = new TestExtendStatic (); // transformation object on
Ts. test ();
}
}
The output is as follows:
========= Static program block ======: parent class static program block
========= Subclass static program block =======: subclass static program block [not a static method]
========= Sub-class main method ========= England: Sub-class main method
========= Method body ========: non-static code block in the parent class
========== Constructor ========: parent class Constructor
========= No name method body =========: non-static code block in the subclass
========= Subclass constructor ========: subclass Constructor
========= Subclass test method ============: subclass Test Method
Execution sequence:Parent static variables and static program blocks --- static variables of sub-classes and static program blocks --- non-static code blocks of the parent class --- constructor in the parent class --- non-static code blocks of sub-classes --- constructor in sub-classes --- the next step is object caller
Method.
As long as the new object is used to create the object and the memory space is allocated, whether it is to assign the reference to the transformation object or to the subclass object, the above method must be executed.
That is:TestStatic ts = new TestExtendStatic (); // transformation object on
TestExtendStatic ts = new TestExtendStatic (); // subclass object
All the above bold programs will be executed.
Ts. as the transformation object, test (); ts calls the methods in the parent class inherited by the subclass. Because test () is overwritten in the subclass, the statements in the subclass are output.
To convert the main method in the subclass to the following:
Copy codeThe Code is as follows: public static void main (String [] args ){
System. out. println ("========= subclass main method ========" + name );
TestStatic ts = new TestExtendStatic ();
Ts. test ();
System. out. println ("-------------------------");
Ts = new TestExtendStatic ();
Ts. test ();
}
Output:
========= Static program block ===== static program block in the parent class
========= Subclass static program block ===== static program block in the subclass
========= Main method of the subclass ======= main method of the subclass of England
========= Method body ======= non-static code blocks in the parent class
========== Constructor ======= constructor in the parent class
========= No name method body ======= non-static program blocks in the subclass
========= Subclass constructor ======= Constructor
========= Subclass test method ========= method of the specific object call
------------------------- Static variables and program blocks are executed only once.
========= Method body ======= non-static code blocks in the parent class
========== Constructor ======= constructor in the parent class
========= No name method body ======= non-static code block in the subclass
========= Subclass constructor ======= Constructor
========= Subclass test method ============
If you change the main method of the subclass:Copy codeThe Code is as follows: TestStatic ts = new TestStatic (); // use the parent class construction method to create
Ts. test ();
Output:
========= Static program block ===== parent static program block
========= Subclass static program block ===== subclass static program block [because the program runs in the subclass, the static program block of the subclass must run]
========= Method body ======= non-static block of the parent class
========== Constructor ======= parent class Constructor
========= Test method ========= specific method of the parent class test ()
If you put the above Code into the parent class, the static program block of the subclass will not be loaded.
We can also find that the static program block runs before the main method, and the non-static program block runs after the main method.
In the primary method of the parent class, I create an object to call test (). The running result is as follows:
========= Static program block ===== static code block
=== Main =
========= Method body ======= non-static code block
========== Constructor ======= Constructor
========= Test method ==========
Summary:
When the program is running (in a class), the static code block that runs is loaded immediately. Once an object is created, the non-static code block and the construction method without parameters are executed. In inheritance, the program will first load the static code block in the parent class and then load its own static code block. Once an object is created (using the subclass construction method ), it will call the non-static code block of the parent class, the constructor of the parent class, and the non-static code block itself.