Class Parent {<br/> static int nstaticvalue; <br/> // + static initialization <br/> static {<br/> system. out. println ("parent static initialization"); <br/> nstaticvalue = 1; <br/>}< br/> //-static initialization </P> <p> int nvalue; <br/> // + initialization <br/> {<br/> system. out. println ("parent initialization"); <br/> nvalue = 2; <br/>}< br/> //-initialization </P> <p> parent (INT nstaticvalue, int nvalue) {<br/> system. out. println ("parent constructor"); <br/>}< br/> class Child extends parent {<br/> static int nchildstaticvalue; <br/> // + static initialization <br/> static {<br/> system. out. println ("Child static initialization"); <br/> nchildstaticvalue = 1; <br/>}< br/> //-static initialization </P> <p> int nchildvalue; <br/> // + initialization <br/> {<br/> system. out. println ("Child initialization"); <br/> nchildvalue = 2; <br/>}< br/> //-initialization </P> <p> child (INT nstaticvalue, int nvalue) {<br/> super (nstaticvalue, nvalue ); <br/> system. out. println ("Child constructor"); <br/>}< br/> @ override <br/> Public String tostring () {<br/> return "nchildstaticvalue =" + nchildstaticvalue + ", nchildvalue =" + nchildvalue; <br/>}< br/> public class testfinalize {
The running result should be:
Parent static Initialization
Child static Initialization
Parent Initialization
Parent Constructor
Child Initialization
Child Constructor
Parent Initialization
Parent Constructor
Child Initialization
Child Constructor
Nchildstaticvalue = 7, nchildvalue = 7
The initialization sequence is:
(1) first, the static variables and static initialization blocks of the parent class are used. (because the main of testfinalize needs to use the Child class, the JVM loads the class and finds that the class inherits from the parent, we will find that the class parent needs to be loaded first, so we will find that the static field of parent is initialized & Static Initialization is executed first, and then the static field of child is initialized & Static Initialization is executed, the execution order of static field and static block is based on the order in which it is written in the Code)
(2) then the static variable of the subclass and the static initialization block.
(3) instance variables and initialization blocks of the parent class (initialization will be executed every time a new object is created. The difference between initialization and constructor is described below)
(4) constructor of the parent class
(5) subclass instance variables and initialization Blocks
(6) subclass Construction Method
Initialization and constructor)
To some extent, it can be understood that the initialization block is a supplement to the constructor, because the initialization is always executed before the constructor, and the object can also be initialized.
Unlike the constructor, the initialization block can only execute a fixed piece of code. It cannot accept parameters like the constructor and initializes all objects of the class.
Like the constructor, during execution, the program not only executes the initialization speed and constructor of the current class, but also traces back to the initialization speed and constructor of the object class. first, execute the initialization block and constructor of the parent class (initialize the block first, then the constructor), and then execute the initialization block and constructor of the current class.