Initialization block: normal initialization block, static initialization block (class initialization block)

Source: Internet
Author: User

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)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.