Subclass a inherits the parent class B,a a=new a ();
The correct order of execution is: parent class B Static code block, subclass a static code block, parent class B non-static code block, parent Class B constructor, subclass a non-static code block, sub-Class A constructor
This means that the execution order of non-static initialization blocks precedes the constructor.
classFatherstatictest {Static{System.out.println ("Executes the static code block of the parent class. "); } fatherstatictest () {System.out.println ("Executes a constructor with no arguments to the parent class. "); } fatherstatictest (String str) {System.out.println ("Executes a constructor with parameters for the parent class. "+str); } { inti = 1; intj = 1; intsum = (i+j); System.out.println ("Executes the construction code block of the parent class. "+sum); }}classSonstatictestextendsfatherstatictest{Static{System.out.println ("Executes a static block of code for the subclass. "); } sonstatictest () {System.out.println ("Executes a subclass without parameters of the constructor method. "); } sonstatictest (String str) {Super(str); System.out.println ("A constructor that executes a subclass with parameters. "+str); } { inti = 2; intj = 2; intsum = (i+j); System.out.println ("Executes the construction code block of the subclass. "+sum); }} Public classSuperclass { Public Static voidMain (string[] args) {NewSonstatictest ("a"); }}
After execution you can see the following order:
executes a static block of code for the parent class. Executes a static block of code for the subclass. Executes the construction code block of the parent class. 2 executes a constructor with parameters for the parent class. A constructs a code block that executes a subclass. 4 The constructor that executes the sub-class with parameters. A
The execution order of static code blocks, non-static code blocks, and construction methods between Java child parent classes