Singleton class

Singleton class  which is created in java for  global point of view. Singletons are useful to provide a unique source of data or functionality to other Java Objects. In android Singleton class creates only one instance.

Benifit  to use of Singleton class in android:

Purpose to use  Singleton class is control to create number of object  creation in project and limiting the number of objects.

Suppose you made a app to play song through service,In this case you can use singleton class concept.Inwhich media player declare in service as a globally and use media player in any where of app,by using concept of singleton class concept.Means you create object of singleton class in activity inwhich you want to use singleton class value .And use mediaplayer globally you don’t need to create another object of media player.

Here is the process to use Singleton class:

This is Arraylist which is globally declare in Singleton class

public ArrayList<String> arraylist  = new ArrayList< String >();
Singleton class is:

// File Name:

    public class Singleton {
private static final Singleton instance = new Singleton();
public ArrayList<SubCatogry_Pozo> arraylist = new ArrayList<SubCatogry_Pozo>();
// /* A private Constructor prevents any other    * class from instantiating.    */
public Singleton() {

/* Static ‘instance’ method */
public static Singleton getInstance() {
return instance;

Here is  SubCatogry_Pozo  is  your pozo class of app.And arraylist which is globally declared in singleton class and use this arraylist through singleton class object.

And then use this singleton class in any activity in project ,To use this Singleton class in another Activity just create object of Singleton class

//Create object of Singleton class
Singleton singleton =Singleton.getInstance( );

And use arraylist through object of Singleton class i.e.

singleton. arraylist.add(value);

value which you want to add in arraylist and here is class inwhich you want to use arraylist of singleton class.

// File Name:

public class MainActivity extends Activity{

Singleton singleton =Singleton.getInstance( );   

protected void onCreate(@Nullable Bundle savedInstanceState) {
   singleton. arraylist.add(value);



The Singleton class maintains a static reference to the  singleton instance and returns that reference from the static getInstance() method.

Be First to Comment

Leave a Reply

Your email address will not be published. Required fields are marked *