先静态再非静态.
静态代码块和静态变量谁先谁后看定义的顺序.
构造代码块先于构造函数执行.
public class MockEnum{
private String id;
private String name;
static {
System.out.println("测试");
}
public final static MockEnum A = new MockEnum("1","2");
public final static MockEnum B = new MockEnum("2","3");
MockEnum(String id, String name){
System.out.println("666");
this.id = id;
this.name = name;
}
{
System.out.println("测试2");
}
public String getName(){
return this.name;
}
public static void main(String[] args) {
new MockEnum("11","22");
}
}
以上代码执行结果为:
测试
测试2
666
测试2
666
测试2
666
public class MockEnum{
private String id;
private String name;
public final static MockEnum A = new MockEnum("1","2");
public final static MockEnum B = new MockEnum("2","3");
MockEnum(String id, String name){
System.out.println("666");
this.id = id;
this.name = name;
}
static {
System.out.println("测试");
}
{
System.out.println("测试2");
}
public String getName(){
return this.name;
}
public static void main(String[] args) {
new MockEnum("11","22");
}
}
以上代码执行结果为:
测试2
666
测试2
666
测试
测试2
666