For example, the question:
1 classfu{2 Static {3System.out.println ("This is a static code block for the parent class");4 }5 {6System.out.println ("This is the parent class construction code block");7 }8 PublicFu () {9System.out.println ("This is the parent class constructor method");Ten } One } A classZiextendsfu{ - Static { -System.out.println ("This is a subclass of static code blocks"); the } - { -System.out.println ("This is a subclass construct code block"); - } + PublicZi () { -System.out.println ("This is a subclass constructor method"); + } A } at classextendstest2{ - Public Static voidMain (String []args) { -Zi z=NewZi (); - } -}
Static code block: loaded as the class loads.
Construct code blocks: each call to a constructor method is performed before the construction method.
When a subclass is loaded, its parent-class Fu is loaded first, i.e.:
In the execution of Zi z=new Zi (); , to load the subclass Zi must first load the parent class Fu, and when the parent class is loaded, the static code block of the parent class loads as the parent class loads------->
System.out.println ("This is a static code block for the parent class");
then the subclass is loaded and the subclass's construction code block is loaded with it------------------------------------------ > System.out.println ("This is a subclass of static code blocks");
Next, create the subclass object:
When the subclass constructor method is called, the parent class constructor method is called, and the parent class constructor method calls the construction code block by default----------> System.out.println ("This is the parent building block of Code");
System.out.println ("This is the parent class constructor method");
When the parent class constructs a method call, the subclass constructor method is called----------------------------------------------> System.out.println ("This is a subclass construction code block");
System.out.println ("This is a subclass construction method");
In Java-static code block, construction code block, construction method of contact