public class Single {
private static Single test=new Single();
private static int counter;
private static int counter1=0;
public Single(){
counter++;
counter1++;
}
public static  Single get(){
return test;
}
public static void main(String[] args){
Single test1=Single.get();
System.out.println(counter);
System.out.println(counter1);
}
}
写出运行结果,以及过程

解决方案 »

  1.   

    1
    1
    一开始会对counter和counter1都初始化为0,调用静态方法get(),get方法会返回静态的test,test会new一个新的Single类的对象出来,new的时候要执行构造方法,在构造方法里,对静态的counter,和counter1自加一.有点乱,不过大概就是这样吧.
      

  2.   

    http://topic.csdn.net/u/20090325/22/ccec797e-9890-45fa-a6d8-3980ce76eeb6.html
      

  3.   

    是1 与0 DEBUG跟踪看到先是两个都初始化为1,然后再由counter1=0得到0>>>?????
      

  4.   

    是这样地>..
    类加载时先对static 域初始化.也就是
    private static Single test=new Single();
       private static int counter;
       private static int counter1=0;
    这按顺序来. 然后因为第一句 private static Single test=new Single(); 调用构造函数,所以接下来就执行构造函数.对counter,counter1赋值.两个都加1,都是1.然后再是接下来的两个static执行,因为counter没有赋值,而counter1有值.为0,所以就是0了.
    可以看出.JAVA默认是将所有的变量赋默认值.对象为NULL, 基本类型为默认值.如整数为0,.....这样.