Java code block

Source: Internet
Author: User

I. Introduction

1. Common code blocks:

Method Body of a method in a class

2. Construct the code block :

The construction block is called when the object is created, and is called each time it is created, prior to the class constructor execution.

3. Static code block:

Code fragments wrapped with static{} are executed only once. Static code blocks take precedence over construction block execution.

4. Synchronizing code blocks:

Using synchronized () {} Wrapped blocks of code, in a multithreaded environment, the read and write operation of the shared data is mutually exclusive, otherwise it will result in inconsistencies in the data. The synchronization code block needs to be written in the method.

two. The similarities and differences between static code blocks and construction code blocks

The same point: after the JVM loads the class and executes before the constructor executes, multiple definitions can be defined in the class, and some static variables are typically assigned in the code block.

Different points: Static code blocks are executed before non-static blocks of code. A static code block executes only once at the first new time, and then not in execution. The non-static code block executes once per new.

Three. Demo 1. Normal code block

The existence of ordinary code blocks can put together code that is called once in the life cycle of a class, and destroy immediately after the program is called, thus saving stack memory and preventing stack overflow.

The order in which ordinary code blocks are executed is in chronological order.

 Public class Test1 {     publicstaticvoid  main (string[] args) {         {             int x = 3;             SYSTEM.OUT.PRINTLN ("variable x= within a common code block" + x);         }          int x = 1;         SYSTEM.OUT.PRINTLN ("variable x= within the Main method" + x);         {             int y = 7;             SYSTEM.OUT.PRINTLN ("variable y= within the normal code block" + y);         }     

Operation Result:

The variable in the normal code block x=3 the variable in the Main method x=1 the variable y in the normal code block=7
2. Building code Blocks

Blocks of code that are defined directly in the class and do not have the static keyword are called {} construction blocks of code.

 Public class Test1 {         {        System.out.println ("construct code block executes first ~ ~ ~");    }          Public Test1 () {        System.out.println ("constructor executes ~ ~ ~ ~ ~");    }          Public Static void Main (string[] args) {                new  Test1 ();        System.out.println ("normal code block last execution ~ ~ ~");    

Operation Result:

Constructs a code block to execute first ~ ~ ~ The constructor executes ~ ~ ~ Ordinary code block last execution ~ ~ ~
3. 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.

 Public classTest1 {StaticString S1 = "CJJ";//Static VariablesString s2 = "blowing quietly";//Common Variables        Static{//Static VariablesSystem.out.println ("Static code block is executed first ~ ~ ~" +S1); }        {//non-static code blockSystem.out.println ("Construct code block re-execute ~ ~ ~" +S2); }         PublicTest1 () {//constructor FunctionSystem.out.println ("constructor re-execution ~ ~ ~ ~"); }         Public Static voidMain (string[] args) {Test1 T=NewTest1 (); System.out.println ("Normal code block last execution ~ ~ ~"); } } 

Operation Result:

The static code block is executed first ~ ~ ~CJJ constructs the code block again executes ~ ~ ~ blows quietly constructs the function to execute ~ ~ ~ Ordinary code block finally executes ~ ~ ~

Attention:

1. Static code blocks cannot exist in any method body.

2. Static code blocks do not have direct access to instance variables and instance methods, and need to be accessed through instance objects of the class

Java code block

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.