`
lufeng4321
  • 浏览: 57933 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

碰到一个不知道的问题,向大家请教

    博客分类:
  • java
阅读更多
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/

package com.racky.singleton;

/**
*
* @author racky
*/
public class Singleton {

    private static Singleton obj=new Singleton();
    private static int counter1;
    private static int counter2=0;
    private Singleton(){
        System.out.println("create instance()");
        counter1++;
        counter2++;
        System.out.println(counter1);
        System.out.println(counter2);
    }
    public static Singleton getInstance(){
        System.out.println("getInstance()");
        return obj;
    }

    public static void main(String args[]){
        Singleton obj=Singleton.getInstance();
        System.out.println("counter1="+obj.counter1);
        System.out.println("counter2="+obj.counter2);
    }
}


请问这段代码的输出结果是什么?为什么会那样?其实结果我是知道的,就是不知道为什么,谁能帮我说说这个问题啊
分享到:
评论
1 楼 theabab 2009-07-14  
static执行次序问题:

先构造函数
count1  0->1
count2  0->1

然后
  count1 没变
  count2 1->0

我觉的是这个问题。static在main函数前运行

相关推荐

Global site tag (gtag.js) - Google Analytics