"Code block" local code block + Construction code block + static code block

Source: Internet
Author: User

1. Local code block

Location: In the method of the class

Presentation method: {}

Function: Control the life cycle of variables, reduce memory consumption

Demo

 Public class Localcode {    publicstaticvoid  main (String [] args) {        int y = ten;        {            int x = +;            SYSTEM.OUT.PRINTLN (x);        }        System.out.println (y);//Run to at this point the X variable has been garbage collected    }}

2. Building code Blocks

Location: in class

Presentation method: {}

Function: Used to initialize each object to reduce the amount of code

Demo

 Public classConstructcode {PrivateString name; {System.out.println ("Construct Code---"); Eat ();//constructor in which the method is extracted and placed in this construction code block} constructcode () {System.out.println ("Drink----");//System.out.println ("Eat---");} constructcode (String name) {//System.out.println ("eat----");SYSTEM.OUT.PRINTLN (name + "Drink----"); }    Private voideat () {System.out.println ("Eat---"); }     Public Static voidMain (String[]args) {Constructcode C=NewConstructcode (); Constructcode Constructcode=NewConstructcode ("XL"); }}

Console output:

"D:\Program Files\java\jdk1.7.0_13\bin\java"-agentlib:jdwp=transport=dt_socket,address=127.0.0.1:59499,suspend= Y,server=n-dfile.encoding=utf-8-classpath "D:\Program Files\java\jdk1.7.0_13\jre\lib\charsets.jar;d:\program Files\java\jdk1.7.0_13\jre\lib\deploy.jar;d:\program Files\java\jdk1.7.0_13\jre\lib\javaws.jar;d:\program Files\ Java\jdk1.7.0_13\jre\lib\jce.jar;d:\program Files\java\jdk1.7.0_13\jre\lib\jfr.jar;d:\program Files\Java\ Jdk1.7.0_13\jre\lib\jfxrt.jar;d:\program Files\java\jdk1.7.0_13\jre\lib\jsse.jar;d:\program Files\Java\jdk1.7.0_ 13\jre\lib\management-agent.jar;d:\program Files\java\jdk1.7.0_13\jre\lib\plugin.jar;d:\program Files\Java\ Jdk1.7.0_13\jre\lib\resources.jar;d:\program Files\java\jdk1.7.0_13\jre\lib\rt.jar;d:\program Files\Java\ Jdk1.7.0_13\jre\lib\ext\access-bridge-64.jar;d:\program Files\java\jdk1.7.0_13\jre\lib\ext\dnsns.jar;d:\program Files\java\jdk1.7.0_13\jre\lib\ext\jaccess.jar;d:\program files\java\jdk1.7.0_13\jre\lib\ext\localedata.jar;d:\ Program Files\java\jdk1.7.0_13\jre\lib\ext\sunec.jar;d:\program files\java\jdk1.7.0_13\jre\lib\ext\sunjce_provider.jar;d:\ Program Files\java\jdk1.7.0_13\jre\lib\ext\sunmscapi.jar;d:\program Files\java\jdk1.7.0_13\jre\lib\ext\zipfs.jar ;D: \cxlwork\stsworkspace\work-test\target\classes;d:\app\repository\org\springframework\spring-core\4.1.6. Release\spring-core-4.1.6.release.jar;d:\app\repository\commons-logging\commons-logging\1.2\ Commons-logging-1.2.jar;d:\app\repository\org\springframework\spring-context\4.1.6.release\ Spring-context-4.1.6.release.jar;d:\app\repository\org\springframework\spring-context-support\4.1.6.release\ Spring-context-support-4.1.6.release.jar;d:\app\repository\org\springframework\spring-beans\4.1.6.release\ Spring-beans-4.1.6.release.jar;d:\app\repository\org\springframework\spring-expression\4.1.6.release\ Spring-expression-4.1.6.release.jar;d:\app\repository\org\springframework\spring-aspects\4.1.6.release\ Spring-aspects-4.1.6.release.jar;d:\app\repository\org\aspectj\aspectjweAver\1.8.5\aspectjweaver-1.8.5.jar;d:\app\repository\org\springframework\spring-aop\4.1.6.release\ Spring-aop-4.1.6.release.jar;d:\app\repository\aopalliance\aopalliance\1.0\aopalliance-1.0.jar;d:\app\ Repository\org\springframework\spring-jdbc\4.1.6.release\spring-jdbc-4.1.6.release.jar;d:\app\repository\org\ Springframework\spring-orm\4.1.6.release\spring-orm-4.1.6.release.jar;d:\app\repository\org\springframework\ spring-tx\4.1.6.release\spring-tx-4.1.6.release.jar;d:\app\repository\org\springframework\spring-web\4.1.6. Release\spring-web-4.1.6.release.jar;d:\app\repository\org\springframework\spring-webmvc\4.1.6.release\ Spring-webmvc-4.1.6.release.jar;d:\app\repository\mysql\mysql-connector-java\5.1.9\ Mysql-connector-java-5.1.9.jar;d:\app\repository\org\apache\logging\log4j\log4j-api\2.4.1\log4j-api-2.4.1.jar ;D: \app\repository\org\apache\logging\log4j\log4j-core\2.4.1\log4j-core-2.4.1.jar;d:\program Files (x86) \ Jetbrains\intellij idea 14.0.3\lib\idea_rt.jar "com.raipeng.work.codeblock.ConstructCodeConnected to the target VM, address:' 127.0.0.1:59499 ', Transport: ' Socket 'disconnected from the target VM, address:' 127.0.0.1:59499 ', Transport: ' Socket 'Construct Code---Eat---Drink----Construct Code---Eat---XL Drink----Process finished with exit code0

3. Static code block

Location: in class

Representation method: static{}

Function: Initializes a class, executes as the class is loaded, and executes only once

Demo

 Public classStaticcode {PrivateString name; Static{System.out.println ("Static"); }     PublicStaticcode () {System.out.println ("Construct"); }     PublicStaticcode (String name) {System.out.println (name+ "Name construct"); }     Public Static voidmain (String [] args) {Staticcode s=NewStaticcode (); Staticcode Staticcode=NewStaticcode ("XL"); }}

The console output is as follows:

Static CONSTRUCTXL   name construct0

"Code block" local code block + Construction code block + static 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.