1.先加载类,静态代码块一起加载
2.如果new实例,按顺序执行构造代码块,然后执行构造函数
3.函数里的代码块,按顺序执行
package name;
public class Hello {
{
System.err.println("构造代码块1");
}
public Hello() {
System.err.println("构造函数");
};
{
System.err.println("构造代码块3");
}
static {
System.err.println("static静态代码块");
}
public static void main(String[] args) {
{
System.err.println("main" + 1);
}
Hello hello = new Hello();
{
System.err.println("main" + 2);
}
}
}
打印:
static静态代码块
main1
构造代码块1
构造代码块3
构造函数
main2