In Java-static code block, construction code block, construction method of contact

Source: Internet
Author: User

 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

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.