本帖最后由 jackhai9 于 2014-8-10 22:01 编辑
大神帮忙分析下这个代码,为什么结果是a=1,b=0? 如果我想输出a=1,b=1,应该怎么改动下?
- class Singleton {
-
- public static Singleton singleton = new Singleton();
- public static int a;
- public static int b = 0;
-
- private Singleton() {
- super();
- a++;
- b++;
- }
-
- public static Singleton GetInstence() {
- return singleton;
- }
- }
-
- public class MyTest {
- public static void main(String[] args) {
- Singleton mysingleton = Singleton.GetInstence();
- System.out.println(mysingleton.a);
- System.out.println(mysingleton.b);
- }
- }
复制代码
|
|