Java code block, common code block, construction code block, static code block

Source: Internet
Author: User

Learn Java in ...

Just learned the Java code block, although very simple or write it down!

left Sound 2016-03-16

-----------------

Execution priority: Static code block > Mian Method > Construct code block > Construction Method

Static code blocks are executed once, regardless of whether they are instantiated, and do not repeat

Executes when constructing a code block instantiation

1, ordinary code block

Ordinary block of code in a method or statement

1PublicClassCodeblock {23PublicStaticvoidMain (string[] args) {45int I; 6  { 7 i = 5; 8 } 9 System.out.println ("i=" +i); 10 }11 }12 13 /*14  run Result: 15 i=516 17 */           

2. Building code Blocks

Constructs a block of code that is defined directly in the class without the Add static keyword

1PublicClassCodeblock {23{4 System.out.println ("Construct code block");5}67Codeblock () {8 System.out.println ("Construction Method"));9}1011 public static void Main (string[] args) {12 codeblock C = new Codeblock (); }14 }15 /*16  running result: 17  construction code block 18  construction method 19 20 */       

3. Static code block

//Construct code blocks defined directly in the class and with the static keyword

1  Public classCodeblock {2     3     {4System.out.println ("Construct code block");5     }6     7     Static{8System.out.println ("Static code block");9     }Ten      One Codeblock () { ASystem.out.println ("Construction Method One")); -     } -      theCodeblock (ints) { -System.out.println ("Construction Method II")); -     } -      +      Public Static voidMain (string[] args) { -         NewCodeblock (); +         NewCodeblock (10); A     } at } -  - /* - Operation Result: - Static code block - Building Code Blocks in Construction Method One - Building Code Blocks to Construction Method Two +  - */

Java code block, common code block, construction code block, static code block

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.