}
class Demo1{
private static Demo1 d = new Demo1();
public static int count1=5;
public static int count2=6;
private Demo1(){
System.out.println(count1+"...构造函数前..."+count2);
count1++;
count2++;
System.out.println(count1+"...构造函数后..."+count2);
}
static{
System.out.println(count1+"...静态代码块前..."+count2);
count1++;
count2++;
System.out.println(count1+"...静态代码块后..."+count2);
}
private static Demo1 h = new Demo1();
public static Demo1 getInstance(){
return d ;
}
}
结果为
0...构造函数前...0
1...构造函数后...1
5...静态代码块前...6
6...静态代码块后...7
6...构造函数前...7
7...构造函数后...8
7.......8