In Java, the object of new class, the static code block inside the class, the non-static code, the parameterless construction method, the parameter construction method, the class general method and so on, their execution order is relatively simple, the program is easy to verify. For example, create a new test parent class.
Public class fathertest {
Private String name;
Fathertest () {
System. out. println ("--the parameterless constructor of the parent class--");
}
Fathertest (String name) {
this. name=name;
System. out. println ("--the parameter constructor of the parent class--" +this. name);
}
Static {
System. out. println ("--static block of code for the parent class--");
}
{
System. out. println ("--non-static block of code for the parent class--");
}
Public void speak () {
System. out. println ("--the method of the parent class--");
}
}
After adding a main program
Public Static void Main (string[] args) {
System. out. println ("--parent main program--");
Fathertest father=New fathertest ("Father's name");
Father.speak ();
}
The result of the execution is:
--The static code block of the parent class--
--Parent class main program--
--Non-static code block of the parent class--
--The father's name--the parameter constructor of the parent class
--the method of the parent class--
It can be apparent that the order of execution: Static code block-Main program-non-static code block-constructor-general method.
If you add the subclass inheritance, the situation becomes more complicated. Let's say we create a new test subclass.
Public class Sontest extends fathertest {
Private String name;
Static {
System. out. println ("--static block of code for subclasses--");
}
{
System. out. println ("--non-static block of code for subclasses--");
}
Sontest () {
System. out. println ("--non-parametric constructors of subclasses--");
}
Sontest (String name) {
this. name=name;
System. out. println ("--the parameter constructor of the subclass--" + this. Name);
}
@Override
Public void speak () {
System. out. println ("--subclasses override the method of the parent class--");
}
}
And then add a main function
Public Static void Main (string[] args) {
System. out. println ("--sub-class main program--");
Fathertest father=New fathertest ("Father's name");
Father.speak ();
Sontest son=New sontest ("The name of the Son");
Son.speak ();
}
The result of the execution is:
--The static code block of the parent class--
--a static block of code for a subclass--
--Sub-class main program--
--Non-static code block of the parent class--
--The father's name--the parameter constructor of the parent class
--the method of the parent class--
--Non-static code block of the parent class--
--the non-parametric constructor of the parent class--
--a non-static block of code for a subclass--
--The name of the son--the parameter constructor of the child class
--Subclasses override the method of the parent class--
After the subclass has been added, the order of execution has changed, and we can summarize it. The first part first executes the static code block of the parent class-the static code block of the subclass-the main program. This part is executed once and is not related to how many objects are built. The second part new has a parent class object and calls the method. Executes its non-static code block-constructor-the general method. The third part new is the object of a subclass and calls the method. The execution order is a non-static code block of the parent class-the parameterless constructor of the parent class, and then the non-static code block of the subclass-the subclass constructor-subclass method.
Java subclass inherits the order of the parent program execution