GeekInterview.com
Series: Subject: Topic:
Question: 3 of 124

What is singleton class?

What is the exact use of singleton class?Just using private class constructor makes it a singleton class?
Asked by: pkmishra09 | Member Since Feb-2011 | Asked on: Sep 21st, 2012

View all questions by pkmishra09

Showing Answers 1 - 3 of 3 Answers
oklaters

Answered On : Sep 29th, 2012

View all answers by oklaters

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.

  
Login to rate this answer.

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

  
Login to rate this answer.
MN Prasad

Answered On : 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.  

  
Login to rate this answer.

Give your answer:

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

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Question Categories

Core Java Interview Questions

EJB Interview Questions

Hibernate Interview Questions

Java Interview Questions

Java Patterns Interview Questions

JDBC Interview Questions

JMS Interview Questions

JSP Interview Questions

Java OOPS Interview Questions

Ruby Interview Questions

Servlets Interview Questions

Spring Interview Questions

Struts Interview Questions

Websphere Interview Questions

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.