Singleton class is use to create one and only one instance of the class. It is use to create singleton database connection. To ensure that no one extends the class and expose the constructor, make the class final.
In software engineering, the singleton pattern is a design pattern that restricts the instantiation of a class to one object. This is useful when exactly one object is needed to coordinate actions across the system. The concept is sometimes generalized to systems that operate more efficiently when only one object exists, or that restrict the instantiation to a certain number of objects. The term comes from the mathematical concept of a singleton.
There is criticism of the use of the singleton pattern, as some consider it an anti-pattern, judging that it is overused, introduces unnecessary restrictions in situations where a sole instance of a class is not actually required, and introduces global state into an application.---As per Wiki
Singleton is one type of Java design pattern in Creational patterns. The main advantage of this pattern is, it is going to return the same object when you called this class. The advantage of this class is, 1)It is not required to create a object for this class to access i.e when you load this class in ClassLoader at that time itself the object is created and when ever you need then it returns the singleton object.
2) Main advantage of this is for example Connection object which is returned by DB is very costly, For example for this first time you want to execute any query then you will get the connection object and after finishing the work you are closing the connection object. Again if you want to execute another query then what you will do is you will get the another connection object from DB.
Here the problem is getting the connection object is very costly and you are calling the same object for multiple times, So we understood that there is problem in creating the same object for multiple times instead of this we have a design pattern called Singleton Design patter to create the object once and when ever you need the connection object(Consider as example here) then we will get from the singleton class instead of creating same object for multiple times.
I think this will you to understand about singleton.
Mayank
Nov 14th, 2014
int can not be null
krishna
Jan 9th, 2015
singleton is class which is allows to create only one object with in the class loader hierarchy.....
A class having only one object or only one object of that class can be created.
Abhishek Marshetty
Nov 18th, 2015
The Singletons purpose is to control object creation, limiting the number of obejcts to one only. Since there is only one Singleton instance, any instance fields of a Singleton will occur only once per class, just like static fields. Singletons often control access to resources such as database connections or sockets.
What is singleton class?
Questions by pkmishra09
Related Answered Questions
Related Open Questions