基本概念:
静态块是在类的加载时候就调用的,静态块只调用一次。
如果一个类中有多个静态对象,按照先后顺序调用。
匿名构造块是在构造函数调用前调用的。
构造函数是在构建函数的时候调用的。
一图直接看懂,直接上代码
public class Gouzao {
public static Gouzao g1 = new Gouzao();
static {
System.out.println("我是静态块");
}
{
System.out.println("我是匿名构造块");
}
public static void cc(){
System.out.println("我是静态方法");
}
Gouzao(){
System.out.println("我是构造函数");
}
public static void main(String[] args) {
new Gouzao();
}
}
执行结果
我是匿名构造块
我是构造函数
我是静态块
我是匿名构造块
我是构造函数