To talk about code blocks.

Source: Internet
Author: User

The code block is divided into 3 cases.
1, Common code block
This is the most common way to write code, and we are most familiar with it, which usually appears in code blocks of methods and statements.

2, building blocks of code
Constructs a block of code that is written directly in the class. It is preferable to the construction method execution, and it is worth reminding that the construction code block is run every time the object is instantiated.

3, Static code block
The essence is a constructed block of code that is decorated with the static keyword, which executes before the main method, runs before the code block is constructed, and differs from the construction code block, regardless of how many times the object is instantiated
are executed only once.


Put a code, let's have a clearer understanding
  1. Package Reviewdemo;
  2. /**
  3. * Test the priority of each code block
  4. * Priority order: Static code block > Construction code block > common code block
  5. * Note: No matter how many objects are created, static code blocks are executed only once!
  6. */

  7. public class Demo13 {
  8. Demo13 () {
  9. System.out.println ("I am the construction Method!") ");
  10. }
  11. {
  12. System.out.println ("I am building blocks of code! ");//Instantiate the object when it is called!
  13. }
  14. static{
  15. System.out.println ("I am a static code block! ");
  16. }

  17. public static void Main (string[] args) {
  18. New Demo13 ();
  19. New Demo13 ();//Create the object again, proving that the static code block executes only once, no matter how many objects are created
  20. System.out.println ("I'm an ordinary code block!") ");
  21. }
  22. }
Copy Code
The result of the operation is:
I am a static code block!
I'm building blocks of code!
I'm the construction method!
I'm building blocks of code!
I'm the construction method!
I am an ordinary code block!

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

To talk about code blocks.

Related Article

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.