Without further ado, Syria, look at the code
package ppt_test; class Root { static{ System.out.println ( "Root static initialization block"); } { System.out.println ( "Root normal initialization block"); } public Root() { System.out.println ( "Root constructor with no arguments"); } } class Mid extends Root { static{ System.out.println ( "Mid static initialization block"); } { System.out.println ( "Mid general initialization block"); } public Mid() { System.out.println ( "Mid constructor with no arguments"); } public Mid(String msg) { // Call overloaded constructor in the same class through this this(); System.out.println ( "Mid constructor parameters with which the parameter values:" + msg); } } class Leaf extends Mid { static{ System.out.println ( "Leaf static initialization block"); } { System.out.println ( "Leaf normal initialization block"); } public Leaf() { // constructor has a super string parameter by calling the parent class super ( "Java initialization sequence demo"); System.out.println ( "Leaf performed constructor"); } } public class TestStaticInitializeBlock { public static void main(String[] args) { new Leaf(); } }
This is the result of running, but why is it so?
The original is because
1. static initialization block is executed only once.
2. Create a sub-type of the object, it will lead to the implementation of static initialization block of the parent type.