Trivial code blocks in Java, building blocks of code, static blocks of code

Source: Internet
Author: User

One: Ordinary code block

1 //Ordinary 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 Occurrence first" 2  Public classcodeblock01{3        Public Static voidMain (string[] args) {4 {5 int x=3; 6 System.out.println ("1, variable x= in normal code block" +x); 7            }            8             intX=1;9SYSTEM.OUT.PRINTLN ("variable x= within the Main method" +x); Ten             { One                intY=7; ASystem.out.println ("2, variable y= in normal code block" +y);  -             } -           } the     }    -     /* - Operation Result: - 1, variable x=3 in normal code block + variable X=1 within the Main method - 2, variable y=7 in normal code block +     */

Two: Building blocks of code

//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 classcodeblock02{ {System.out.println ( "first building block"); }        PublicCodeBlock02 () {System.out.println ("Construction Method"); } {System.out.println ( "second building block"); }     Public Static voidMain (string[] args) {NewCodeBlock02 (); NewCodeBlock02 (); NewCodeBlock02 (); }}    /** Execution Result: First building block construction method of the second construction block the first building block construction method of the second construction block the first building block construction method of the second building block*/

Three: 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. classcode{{System.out.println ("Code's Building Block"); }        static{System.out.println ("code static Block"); }            PublicCode () {System.out.println ("How to Construct code"); }}     Public classcodeblock03{{System.out.println ("Building Blocks of CodeBlock03"); }         static{System.out.println ("Static code block for CodeBlock03"); }             PublicCodeBlock03 () {System.out.println ("How to construct CodeBlock03"); }             Public Static voidMain (string[] args) {System.out.println ("The Main method of CodeBlock03"); NewCode (); NewCode (); NewCodeBlock03 (); NewCodeBlock03 (); }}/*CodeBlock03 Static code block CodeBlock03 The main method code of the static block code of the building block code construction Method Code of the construction block Code construction method CodeBlock03 construction of the building block CodeBlock03 method CodeBlock0 Construction method of building block CodeBlock03 of 3*/

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.

Trivial code blocks in Java, building blocks of code, static blocks of code

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.