静态代码块

package objectOriented;

 * @param args
 * 静态代码块
 * 格式:
 * static
 * {
 *         静态代码块中的执行语句。
 * }
 * 特点:随着类的加载而执行,只执行一次。
 *              用于给类进行初始化的。
 *
 * 下例执行结果:
 * b c a f f over

class StaticCode{
    StaticCode(){
        System.out.println("f");
    }
    static{
        System.out.println("a");
    }
}

public class StaticBlock {
    static{
        System.out.println("b");
    }
   
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        new StaticCode();
        new StaticCode();
        System.out.println("over");
        StaticCode s = null; //此句没有加载类StaticCode,不会打印a
    }
    static{
        System.out.println("c");
    }

}

  

posted @ 2013-08-02 10:46  微风夜明  阅读(104)  评论(0编辑  收藏  举报