在Android开发中 我们经常会用到单例模式 下面这个是经常用到而且代码比较健壮的写法
public class DemoInstance{ private volatile static DemoInstance instance; /** Returns singleton class instance */ public static DemoInstance getInstance() { if (instance == null) { synchronized (DemoInstance.class) { if (instance == null) { instance = new DemoInstance(); } } } return instance; }
供大家参考一下