Static code blocks, construction code blocks, execution of constructors

Source: Internet
Author: User

1  Public classSTR {2 3     Static {4System.out.println ("Static{1}");5     }6     Static {7System.out.println ("Static{2}");8     }9     {TenSystem.out.println ("{}"); One     } A  - Str () { -System.out.println ("Str ()"); the     } -  -      Public Static voidMain (string[] args) { -         NewStr (); +         NewStr (); -     } +}

Output Result:

Static{1}
STATIC{2}
{}
STR ()
{}
STR ()

Conclusion:

1. Static code blocks are executed sequentially and executed once (class loading is performed)

2. Constructs a code block, executes each time the new object is executed, and executes before the constructor

3. Execute the constructor

Understand:

Static code blocks are static, owned by the class, executed when the class is loaded, and construction blocks are non-static, owned by the object, and executed when the object is constructed.

Static code blocks, construction code blocks, execution of constructors

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.