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