下面这段程序的打印结果是:
obj.counter1==1
obj.counter2==0
百思不得其解,counter1没有附初值,默认值也是0啊,为什么他们两个的结果不一样呢?
请大侠讲解下其中的原因,谢谢!
public class Singleton {
private static Singleton obj = new Singleton();
public static int counter1;
public static int counter2 = 0;
private Singleton() {
counter1++;
counter2++;
}
public static Singleton getInstance() {
return obj;
}
public static void main(String[] args) {
Singleton.getInstance();
System.out.println("obj.counter1==" + counter1);
System.out.println("obj.counter2==" + counter2);
}
} |
posted on 2008-10-28 15:26
杨爱友 阅读(1373)
评论(9) 编辑 收藏 所属分类:
java相关技术