The ClassicSingleton class maintains a static reference to the lone singleton instance and returns that reference from the static getInstance() method.
//call from java class
Singleton singleton = Singleton.getInstance();
//class
public class Singleton { private static Singleton mInstance = null; private String mString; private Singleton() { mString = "Hello"; } public static Singleton getInstance() { if (mInstance == null) { mInstance = new Singleton(); } return mInstance; } // for clear instance public static void clearInstance() { mInstance = null; } public String getString() { return this.mString; } public void setString(String value) { mString = value; } }
No comments:
Post a Comment