朋友, 你对java很熟吗? 来看看下面这段代码, 心里运行一次, 看看它的执行结果是什么? 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; } }
// 程序2 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); } } 想出来结果了吗? 那你把这段程序编译运行一下, 看看是不是你想的答案?