码迷,mamicode.com
首页 > 其他好文 > 详细

【code block】局部代码块+构造代码块+静态代码块

时间:2015-11-18 14:01:57      阅读:159      评论:0      收藏:0      [点我收藏+]

标签:

1.局部代码块

  位置:位于类的方法中

  表示方法:{}

  作用:控制变量的生命周期,减少内存消耗

  demo:

public class LocalCode {
    public static void main(String [] args){
        int y = 10;
        {
            int x = 20;
            System.out.println(x);
        }
        System.out.println(y);//运行到此时x变量已被垃圾收集
    }
}

2.构造代码块

   位置:位于类中

   表示方法:{}

  作用:用于给每个对象进行初始化,减少代码量

  demo:

public class ConstructCode {
    private String name;
    {
        System.out.println("construct code---");
        eat();//构造器中都有该方法中,抽取出来放在此构造代码块中
    }
    ConstructCode(){
        System.out.println("drink----");
//        System.out.println("eat---");
    }
    ConstructCode(String name){
//        System.out.println("eat----");
        System.out.println(name +"   drink----");
    }
    private void eat(){
        System.out.println("eat---");
    }
    public static void main(String[]args){
        ConstructCode c = new ConstructCode();
        ConstructCode constructCode = new ConstructCode("xl");
    }
}

控制台输出:

"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.ConstructCode
Connected 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 code 0

3.静态代码块

     位置:类中

    表示方法:static{}

    作用:给类进行初始化;随着类的加载而执行,并且只执行一次

  demo:

public class StaticCode {
    private String name;
    static {
        System.out.println("static");
    }
    public StaticCode(){
        System.out.println("construct");
    }
    public StaticCode(String name){
        System.out.println(name+"   name construct");
    }
    public static void main(String [] args){
        StaticCode s  = new StaticCode();
        StaticCode staticCode = new StaticCode("xl");
    }
}

控制台输出如下:

static
construct
xl   name construct


Process finished with exit code 0

 

【code block】局部代码块+构造代码块+静态代码块

标签:

原文地址:http://www.cnblogs.com/bg2015-07-05/p/4974096.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!