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 class MyMain
{
public static void main(String[] args)
{
Singleton obj = Singleton.getInstance();
System.out.println("obj.counter1=="+obj.counter1);
System.out.println("obj.counter2=="+obj.counter2);
}
}
执行结果是:
obj.counter1==1
obj.counter2==0
counter1和counter2的值为什么不相等呢?{:3_46:}
|
|