
Singletons are useful to provide a unique source of data or functionality to other Java Objects. Design pattern singleton is used to control the heap overflow. It limits the user to create one object for a particular class. If we need to add more functionalities then we can create more objects.
Singleton is a class which has only one instance in whole application and provides a public static method to access the singleton instance.
The implementation involves a static member in the “Singleton” class, a private constructor and a static public method that returns a reference to the static member. A singleton is a class that is instantiated only once. It has a private constructor so that the class can’t be instantiated by outside classes.
Example –
Class EmployeeSalery{
Private EmployeeSalery(){
// class implementation
}
Private static EmployeeSalery empobj;
Public static getEmployeeObject(){
If(empobj==null)
empobj=new EmployeeSalery();
Return empObj;
// You can also use this to create object
// return empobj == null ? empobj=new EmployeeSalery() : empobj;
}
}
Related Posts...
Mobile AppsTechnologiesWeb Design
Mar 20th, 2025
In today’s fast-paced world, convenience is king. Whether it’s groceries, meals, or even medicines, Quick Commerce or Q-Commerce apps have revolutionised the way we shop and receive goods. These apps […]
Read more
Mar 18th, 2025
In the fast-paced world of technology, mobile apps have become an integral part of our daily lives. From ordering food to managing finances, apps are everywhere. But have you ever […]
Read more
Mar 13th, 2025
Singapore’s startup ecosystem is a beacon of innovation, consistently ranking among the top global hubs for technology and entrepreneurship. As of 2025, Singapore holds the 7th position worldwide, boasting an […]
Read more