Friday, June 15, 2012

Singleton classes in java

Speaking in a layman's language "classes that stay single throughout their life are called singleton classes" is not wrong at all
As you cant be a layman if you have surfed through here to read about singleton classes.
Here is the technical description for Singleton classes in java:

Classes with their constructor declared as private is called singleton class,simply speaking.

>> As we are aware about the purpose of constructors and consequences of declaring constructor for a class as private.


is a kind of method inside a class with same identifier(name) as of class,constructor is invoked by default whenever we create an object for a class.There can be multiple constructors declared for a single class,each with different no of arguments.
constructors are mostly used to initialize the member variables of the class by using constructors with arguments.

Suppose if there is only one constructor which is declared as private, means what ?
if you know a little about the access specifiers public , private and protected you will say "if the constructor is declared as private, we can't invoke it from outside of the class" 
that's right !!
to create an object of a class we need to call its constructor infact the default constructor is called automatically.
when we write something like
                                                 ClassName obj=new ClassName;
>>This simply concludes that we cant create object from outside of a class with private constructor.

But there is a facility by which we can create a object for a singleton class but keep in mind only one object not more than one !!

Using static bock because static functions of a class can be called from anywhere using the class name even if there is no object existing for the class.
like shown in the java code snippet below:

Class Singleton
         int a;
         String s;
         static Singleton obj;
         private singleton()
        public static singleton getobject()
               return(new singleton());
               System.out.println("object of singleton class already exist");
        public static displaydata()
               System.out.println("a ="+a);
               System.out.println("s ="+s);

Class test
          public static void main(String args[])
                  Singleton obj=singleton.getobject();

object of singleton class already exist

>> whenever a static member of a class is called from outside of the class the class is automatically loaded into the memory.
>> The static content of a class is loaded only once into the memory even if we try to load a class multiple times. 
so in above example:
-> there is a class with name Singleton with private constructor.
-> there is a static member variable obj  of Class Singleton type.
-> when we are calling getobject() method first time from main method present in Test  class,JVM(java virtual machine) look into the memory for class Singleton,if it is already present in the memory or not.
but it is first time we are loading it so JVM will load the class and its static members into the memory with no problem.
-> Now the class is instantiated and the object is returned to the calling function main.
-> If You will try again to call the getobject() method to prove me wrong you   will see a message on the screen
"object of singleton class already exist"

So this is the way singleton classes are implemented and instantiated in realtime applications.

Now you must be thinking !!!
What is the use of defining a singleton class when we can't create objects to use its member functions


We make our class singleton, when our class is to be used by multiple threads of an application.Because if all the threads are allowed to create a separate object of class for themselves then logically they will be working on different set of variables and cant solve the purpose of multiple threading.
For Example: say there is an online shopping website,what they do.They have a products catalog in which all the products which are available in their stock are present and this product catalog is nothing but a class(singleton class).
This product catalog class is shared between every customer(a thread speaking technically) on the website to look for their favorite product because the product catalog is same for every customer,it will not be a good idea if they load the same class separately for each customer.


No comments:

Post a Comment