Creating a Singleton Class in Java

Creating a Singleton Class in Java

Assume you want a class for which only one object can be created. This type of class is known as Singleton Class. How can you create that class? Before jumping to this topic let me recall the way for creating an object. The method is:

className_object_name=new className();

Now what does className() after new keyword means? It means, for object creation the constructor for that class is called. So, for stopping others from creating object we can do one thing, we can make constructor private! Now no one outside the class can call the constructor hence cannot create objects. But this doesn’t solve the problem! We want a class which could create only one object! Now how can we achieve this? Let’s see!

Before this let me recall you that when we want to create some property which is common to the whole class and for accessing it we don’t need to create object.

Let’s firstly create a class and privatize the constructor!

static class Singleton{
      private Singleton(){

      }

}

Now you can check that you are not able to create an object for this class!

What we need? An object of this class! Let’s just declare one object in this class!

static class Singleton{

      private Singleton(){



      }

      Singleton obj;



}

Now we want to return this object. But the condition that this should be the only object! Also, the object shouldn’t be accessed outside the class because if it is accessible then we can create more than one object by using this object. So we should also privatize the Object. How can achieve returning an object? By a function! So, create a function, say getObj, In that function we have to create a condition that if there is no object of this class then an object should be created. We can use an if condition for this.

static class Singleton{

      private Singleton(){



      }

     private Singleton obj;

   Singleton getObj(){

       if(obj==null){

           Singleton obj=new Singleton();

       }

       return obj;

   }

}

It should be fine! Isn’t it? But wait we will not create an object for this class, we will indirectly call the function for creating object, but for that function should be static, because we are not going to create the object. So shouldn’t the object we created be also static? Yes, because the function is static. So, the most accurate code is:

static class Singleton{

      private Singleton(){



      }

     private static Singleton obj;

   static Singleton getObj(){

       if(obj==null){

           Singleton obj=new Singleton();

       }

       return obj;

   }

}

So, how can we now create an object? This way:

public class oops {

    public static void main(String[] args) {

      Singleton objOld=Singleton.getObj();

    }

  static class Singleton{

        private Singleton(){



        }

       private static Singleton obj;

     static Singleton getObj(){

         if(obj==null){

             Singleton obj=new Singleton();

         }

         return obj;

     }

  }

}

What will happen when we create another object, say objNew? It will return objOld! Why? Because now the obj in Singleton is objOld, so it will not enter if condition, as it is not null. Hence at the end obj which is = objOld will be returned.

This is the way we create a Singleton Class. I hope this blog made you clear your concepts. Feedbacks will be appreciated.