class SingleTon {
private static SingleTon singleTon = new SingleTon();
public static int count1;
public static int count2 = 0;
private SingleTon() {
count1++;
count2++;
}
public static SingleTon getInstance() {
return singleTon;
}
}
public class Test {
public static void main(String[] args) {
SingleTon singleTon = SingleTon.getInstance();
System.out.println("count1=" + singleTon.count1);//count1=1
System.out.println("count2=" + singleTon.count2);//count2=0
}
}
package com.test.classload;
class AAA {
public static int count1;
public static int count2 = 0;
public AAA() {
count1++;
count2++;
}
}
public class Test2 {
public static void main(String[] args) {
AAA aaa = new AAA();
System.out.println("count1=" + aaa.count1);//count1=1
System.out.println("count2=" + aaa.count2);//count2=1
}
}
why?
http://www.tuicool.com/articles/QZnENv 这篇解释看的不是很懂~~~
欢迎来到这里!
我们正在构建一个小众社区,大家在这里相互信任,以平等 • 自由 • 奔放的价值观进行分享交流。最终,希望大家能够找到与自己志同道合的伙伴,共同成长。
注册 关于