JAVA 静态代码块

示例代码:

public class Test01 {
    public Test01(){
        System.out.println("this is constructor code");    //构造函数代码块
    }
    {
        System.out.println("this is construction code");    //构造代码块
    }
    static {
        System.out.println("this is static code");    //静态代码块
    }

    public static void main(String[] args) {
        new Test01();
        new Test01();
    }
}

运行结果:

this is static code
this is construction code
this is constructor code
this is construction code
this is constructor code

可以看到显示static代码初始化,然后是构造方法初始化,然后是构造函数初始化,并且静态代码只会初始化一次。

原文地址:https://www.cnblogs.com/stilldream/p/15663479.html