Initialization process: 1. Initializes static member variables and static code blocks in the parent class; 2. Initializes static member variables and static code blocks in subclasses; 3. Initializes the normal member variables and code blocks of the parent class, and then executes the parent class's construction method; 4. Initializes the normal member variable and code block of the subclass, and then executes the subclass construction method;
classx{y y=NewY (); PublicX () {System.out.print (X); }}classy{ PublicY () {System.out.print (Y); }} Public classZextendsx{y y=NewY (); PublicZ () {System.out.print (Z); } Public Static voidMain (string[] args) {NewZ (); }}
(1) Initialize the normal member variables and code blocks of the parent class and Execute y y=new y (); The Output Y (2) Executes the constructor of the parent class, and the output X (3) initializes the normal member variable and code block of the subclass, executing y y=new y (); The output y (4) then executes the sub-class construction method; output Z
Java Initialization process