Wednesday, 23 March 2016

How to use Singleton Class ?



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

Run/install/debug Android applications over Wi-Fi ?

Open Teminal/cmd --------------- Below steps is for Android 10 or lower Step 1 - Connect the device via USB and make sure debugging is work...