1 /*2 initialization block: normal initialization block, static initialization block (class initialization block)3 4 The following program creates three classes: Root,mid and Leaf,5 three classes provide static initialization blocks and normal initialization blocks,6 and the mid class still uses this to call the overloaded structure area, and7 the leaf uses the super display to invoke the constructor specified by the parent class8 */9 classRootTen { One Static{ ASYSTEM.OUT.PRINTLN ("Static initialization block of root"); - } - the { -System.out.println ("Root's normal initialization block"); - } - PublicRoot () + { -SYSTEM.OUT.PRINTLN ("Root parameter-free constructor"); + } A } at - classMidextendsRoot - { - Static{ -SYSTEM.OUT.PRINTLN ("Static initialization block for mid"); - } in - { toSYSTEM.OUT.PRINTLN ("Normal initialization block for mid"); + } - PublicMid () the { *SYSTEM.OUT.PRINTLN ("parameter-free constructor for mid"); $ }Panax Notoginseng PublicMid (String msg) - { the //calling overloaded constructors in the same class through this + This(); ASYSTEM.OUT.PRINTLN ("The parameter constructor for mid, whose parameter value is:" +msg); the } + } - $ classLeafextendsMid $ { - Static{ -SYSTEM.OUT.PRINTLN ("Static initialization block of the leaf"); the } - Wuyi { theSYSTEM.OUT.PRINTLN ("Plain initialization block of the leaf"); - } Wu - PublicLeaf () About { $ //A constructor that invokes a string argument in the parent class through Super - Super("Crazy Java Handout"); -System.out.println ("The constructor that executes the leaf"); - } A } + the classTest - { $ Public Static voidMain (string[] args) the { the NewLeaf (); the NewLeaf (); the } - } in /* the Run as follows: (a good understanding of understanding) the ----------Run---------- About static initialization block of root the static initialization block for mid the static initialization block of the leaf the common initialization block for root + The parameterless constructor of root - normal initialization block for mid the parameter-free constructors for midBayi The parameter value of the constructor for mid is: Crazy Java handout the ordinary initialization blocks of the leaf the the constructor that executes the leaf - common initialization block for root - The parameterless constructor of root the normal initialization block for mid the parameter-free constructors for mid the The parameter value of the constructor for mid is: Crazy Java handout the ordinary initialization blocks of the leaf - the constructor that executes the leaf the the output complete (0 seconds)-Normal termination the */
Initialization block: normal initialization block, static initialization block (class initialization block)