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
May 7th, 2024
Have you ever considered how convenient it would be to order groceries online and deliver them to your doorstep? This has become more popular, especially after the COVID-19 pandemic. People […]
Read more
May 2nd, 2024
Will you be ready for the software development landscape in 2024? Expert insights on the top 15 trends – from VR collaboration to quantum computing – that will shape the future in this forward-looking Developer’s Roadmap.
Read more
Apr 30th, 2024
Election Evolution: The Rise of Political Campaign Management App Development – From lawn signs to social media, campaign app development marks a new era in election strategy. This blog explores the evolution of campaign tech.
Read more