Common code blocks in Java, building blocks of code, static code block differences, and code examples

Source: Internet
Author: User

Common code blocks in Java, building blocks of code, static code block differences, and code examples from: http://www.cnblogs.com/sophine/p/3531282.html

Execution order: (Priority from high to low.) Static code block >mian method > construct code block > Construct method.

Where static code blocks are executed only once. The construction code block executes every time the object is created.

1 Common code blocks

1//Common code block: {} that appears in a method or statement is called a normal code block. Ordinary blocks of code and general execution order of statements are determined by the order in which they appear in the code--"first occurs first" 2 public class codeblock01{3 public       static void Main (string[] args) {4            5             {6               int x=3; 7               System.out.println ("1, variable x= in normal code block" +x);     8             } 9             int x=1;11             System.out.println ("variable x= within the Main method" +x); {+                int y=7;15                System.out.println ("2, variable y= in normal code block" +y);             }17           }18     }19     /*21     operation Result:     1, the variable in the normal code block x=323 the          variable x=124 in the Main method          2, Variable y=725 in normal code block     */26     

2 Building Code Blocks

Building blocks: Blocks of code that are defined directly in the class and do not have the static keyword are called {} construction blocks of code. Construction blocks are called when the object is created, each time the object is created, and the order in which the code block is constructed takes precedence over the class constructor. public class codeblock02{    {      System.out.println ("first code block");        }        Public CodeBlock02 () {        System.out.println ("constructor method");        }                {          System.out.println ("second building Block");        }      public static void Main (string[] args) {          new CodeBlock02 ();          New CodeBlock02 ();          New CodeBlock02 ();               }}    /** Execution Result: first code block second construction block construction method first code block second construction block construction method first code block construction method of the second construction block */

3 Static code block

Static code block: a block of code declared in Java using the static keyword. Static blocks are used to initialize the class and initialize the properties of the class. Each static block of code is executed only once. Because the JVM executes static blocks of code when the class is loaded, the static block of code executes before the main method. If a class contains more than one static block of code, it will follow the code defined first, then execute after the definition. Note: 1 Static code blocks cannot exist in any method body. 2 Static code blocks do not have direct access to static instance variables and instance methods and need to be accessed through instance objects of the class.    Class Code{{System.out.println ("Code building Block");        } static{System.out.println ("code static Block");        } public code () {System.out.println ("code construction Method");         }} public class codeblock03{{System.out.println ("CODEBLOCK03 building Block");        } static{System.out.println ("CodeBlock03 Static code block");            } public CodeBlock03 () {System.out.println ("construction Method of CodeBlock03");            } public static void Main (string[] args) {System.out.println ("Main method of CodeBlock03");            New Code ();            New Code ();            New CodeBlock03 ();          New CodeBlock03 (); }}/*codeblock03 Static code block CodeBlock03 The main method code static code blocks code construction block Code construction Method Code construction block code construction Method CODEBLOCK03 building block CodebThe construction method of the construction block CodeBlock03 of the Lock03 method CodeBlock03 */ 

Common code blocks in Java, building blocks of code, static code block differences, and code examples

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.