What is singleton class?

What is the exact use of singleton class?Just using private class constructor makes it a singleton class?

Questions by pkmishra09

Showing Answers 1 - 8 of 8 Answers

oklaters

  • Sep 29th, 2012
 

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.

  Was this answer useful?  Yes

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


Normally we do it

Code
  1. public class SingletonDemo {

  2. private static volatile SingletonDemo instance = null

  3.  

  4. private SingletonDemo() { }

  5.  

  6. public static SingletonDemo getInstance() {

  7. if (instance == null) {

  8. synchronized (SingletonDemo .class){

  9. if (instance == null) {

  10. instance = new SingletonDemo ()

  11. }

  12. }

  13. }

  14. return instance

  15. }

  16. }


in this manner but

Code
  1. class SingletonClass

  2. {

  3. private static int count=0

  4. SingletonClass() throws Exception

  5. {

  6. if(count==0)

  7. {

  8. System.out.println("Object Created.")

  9. count++

  10. }

  11. else throw new Exception("Singleton Class cannot have more than one Object.")

  12. }

  13. public static void main(String args[])

  14. {

  15. try{

  16. SingletonClass s1=new SingletonClass()

  17. SingletonClass s2=new SingletonClass()

  18. SingletonClass s3=new SingletonClass()

  19. }catch(Exception e)

  20. {

  21. System.out.println("Exception: "+e.getMessage())

  22. }

  23. }

  24. }



in this manner by using the if condition you can create n-ton class

  Was this answer useful?  Yes

MN Prasad

  • Dec 29th, 2012
 

Singleton class is used for only one instance is created for entire application. One instance and multiple threads.

Code
  1. class Singleton

  2. {  

  3.    private static int singleton;

  4.    private Singleton()

  5.    public static Singleton getUniqueInstanceID()

  6.      {

  7.         if (singleton== null)

  8.           {

  9.              singleton = new Singleton();

  10.           }

  11.    }

  12. }

  13.  

  Was this answer useful?  Yes

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.

  Was this answer useful?  Yes

Mayank

  • Nov 14th, 2014
 

int can not be null

  Was this answer useful?  Yes

krishna

  • Jan 9th, 2015
 

singleton is class which is allows to create only one object with in the class loader hierarchy.....

  Was this answer useful?  Yes

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.

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions