Public class person {A = 6; // if (a> 4) {system. out. println ("person init Block A> 4") ;}} public int A = 9; {system. out. println ("second executed");} static {// The system is executed even if the object is not instantiated. out. println ("person static block");} public static void main (string [] ARGs) {// person P = new person (); // system. out. println ("A" + P. a); leaf L = new leaf (); leaf LL = new leaf () ;}} class root {static {system. out. println ("root static init");} {system. out. println ("root normal init");} public root () {system. out. println ("root constructor");} class mid extends root {static {system. out. println ("mid static init") ;}{ system. out. println ("mid normal init");} public mid () {system. out. println ("mid constructor") ;}} class leaf extends mid {static {system. out. println ("leaf static init");} {system. out. println ("leaf normal init");} public leaf () {system. out. println ("leaf constructor ");}}
Output result:
Person static Block
Root static init
Mid static init
Leaf static init
Root normal init
Root Constructor
Mid normal init
Mid Constructor
Leaf normal init
Leaf Constructor
Root normal init
Root Constructor
Mid normal init
Mid Constructor
Leaf normal init
Leaf Constructor
Initialization sequence:
Static initialization block (parent class ---> subclass) ----> non-static initialization block of the parent class (declaring the default value of the attribute instance (the execution sequence is determined by the sequence in which the initialization block and default value are declared )) -----> constructor of the parent class -------> non-static initialization block of the subclass (declaring the default value of the attribute instance (the execution sequence is determined by the sequence in which the initialization block and default value are declared )) ---------> constructor of subclass