1 class X { 2 y b = new y (); 3 x () { 4 system.out.print ("X"); 5 } 6 } 7 8 class y { 9 y () { 10 system.out.print ("Y"); 11 } 12 } 13 14 public class z extends x { &nbsP;15 y y = new y (); 16 z () { 17 system.out.print ("Z"); 18 } 19 20 public static void main (String[] args) { 21 new z (); 22 } 23 }
Class before calling the constructor, first executes the statement that initializes the class member, and if other classes are inherited, the constructor of the parent class is called before the class member initialization statements of this class are executed, so the order of execution is (identified by the line number): 21,16,3,2,9,10 (Y), 3,4, (X), 16,15,9,10 (Y), 16,17 (Z), and then proceeds to the end. () The output of the character in the line, so the result is yxyz.
Attached: The JDB command is a Linux gdb-like debug command that comes with the JDK and can be run on the command line, as detailed in the documentation.
Java class Initialization process