对单例模式理解上的问题..............
public class SingLeton{ private static SingLeton instance = new SingLeton(); public static SingLeton getInstance(){ return instance; } } public class Singleton { private static Singleton instace = null; // private 修饰,避免外部更改 private Singleton(){} // 隐藏构造函数 public static synchronized Singleton getInstance() { // 同步,避免同时创建多个实例 if (instance == null) { instance = new ... } return instance; }}
[解决办法]
public class Singleton{ private static Singleton instance = new Singleton(); private SingLeton(){ } public static Singleton getInstance(){ return instance; }}