首页 > 代码库 > 单例模式之JAVA实现

单例模式之JAVA实现

 

class Singleton {    private static Singleton instance = null;    private Singleton()    {        System.out.println("Init the Singleton instance!");    }    public static Singleton getInstance() {        if (null == instance)            instance = new Singleton();        return instance;    }}public class SingletonPattern {    /**     * @param args     */    public static void main(String[] args) {        Singleton instanceA = Singleton.getInstance();        Singleton instanceB = Singleton.getInstance();                if (instanceA == instanceB) {            System.out.println("They are the same instance!");        }    }}

 

单例模式之JAVA实现