What is singleton class?

Answers were Sorted based on User's Feedback

Answer / sathya

Singleton class:
This is a class which can be instatiated only once.


Public class Singleton
private static single = new Singleton();

Private Singleton();

For a singleton class, the constructor is made private and
a static variable is used for instatiating the class.

Is This Answer Correct ?    240 Yes 44 No

Answer / ranganathkini

A Singleton is a class which at any given time has only one
instance and it does not allow more instances to be created.
Such classes do not have an accessible constructor, for example:

public class Singleton {
// class is automatically instantiated when the
// class is loaded
private static Singleton instance = new Singleton()

// constructor is made inaccessible by declaring
// it private
private Singleton() { ... }

// Access to the single instance of the class is
// provided by a static accessor method
public static Singleton getInstance() {
// returns a reference of the private instance
return instance;

// rest of the class implementation

Is This Answer Correct ?    176 Yes 21 No

Answer / test

In computer science the singleton design pattern is designed
to restrict instantiation of a class to one (or a few)
objects. This is useful when exactly one object is needed to
coordinate actions across the system. Sometimes it is
generalized to systems that operate more efficiently when
only one or a few objects exist.

The singleton pattern is implemented by creating a class
with a method that creates a new instance of the object if
one does not exist. If one does exist it returns a reference
to the object that already exists. To make sure that the
object cannot be instantiated any other way the constructor
is made either private or protected.

The singleton pattern must be carefully constructed in
multi-threaded applications. If two threads are to execute
the creation method at the same time when a singletondoes
not yet exist they both must check for an instance of the
singleton and then only one should create the new one.

The classic solution to this problem is to use mutual
exclusion on the class that indicates that the object is
being instantiated.

A Java programming language solution is as follows. It is
based on the Q&A link found below modified for
multi-threading however it is still vulnerable to the
double-checked locking anti-pattern also found below:

public class Singleton {
private static Singleton INSTANCE null;

// Private constructor suppresses
// default public constructor
private Singleton() {}

//synchronized creator to defend against multi-threading issues
//another if check here to avoid multiple instantiation
private synchronized static void createInstance() {
if (INSTANCE null) {
INSTANCE new Singleton();

public static Singleton getInstance() {
if (INSTANCE null) createInstance();
return INSTANCE;

Is This Answer Correct ?    25 Yes 3 No

Answer / akshat maheshwari

A singleton is an class that can be instantiated once, and
only once. This is a fairly unique property, but useful in a
wide range of object designs. Creating an implementation of
the singleton pattern is fairly straightforward - simple
block off access to all constructors, provide a static
method for getting an instance of the singleton, and prevent

Is This Answer Correct ?    19 Yes 3 No

Answer / vimal eldose george

Singleton gives only one instance.

class Single{

static int objCreated = 0;
private Single(){
public static Single getThis(){
return new Single();
return this;


Is This Answer Correct ?    32 Yes 17 No

Answer / abhijith thette nagarajan

As per the definition,Singleton class in one such class
which will have only one instance of it at any given point
of time...

But,it is allowed to any number of instances,not
necessarily be one..

Generally its contructor will have "protected" key work,no
problem also if we have private also,but if it is
private,we need to have a getter method to create an

Is This Answer Correct ?    34 Yes 20 No

Answer / srinivas pentakota

A singleton is an class that can be instantiated once, and
only once. This is a fairly unique property, but useful in a
wide range of object designs. Creating an implementation of
the singleton pattern is fairly straightforward - simple
block off access to all constructors, provide a static
method for getting an instance of the singleton, and prevent

public class SingletonObject
private SingletonObject()
// no code req'd

public static SingletonObject getSingletonObject()
if (ref == null)
// it's ok, we can call this constructor
ref = new SingletonObject();
return ref;

private static SingletonObject ref;

Is This Answer Correct ?    27 Yes 14 No

Answer / venu

The Singleton is a useful Design Pattern for allowing only
one instance of your class, but common mistakes can
inadvertently allow more than one instance to be created. In
this article, I'll show you how that can happen and how to
avoid it.

The Singleton's purpose is to control object creation,
limiting the number to one but allowing the flexibility to
create more objects if the situation changes. Since there is
only one Singleton instance, any instance fields of a
Singleton will occur only once per class, just like static

Singletons often control access to resources such as
database connections or sockets. For example, if you have a
license for only one connection for your database or your
JDBC driver has trouble with multithreading, the Singleton
makes sure that only one connection is made or that only one
thread can access the connection at a time. If you add
database connections or use a JDBC driver that allows
multithreading, the Singleton can be easily adjusted to
allow more connections.

Moreover, Singletons can be stateful; in this case, their
role is to serve as a unique repository of state. If you are
implementing a counter that needs to give out sequential and
unique numbers (such as the machine that gives out numbers
in the deli), the counter needs to be globally unique. The
Singleton can hold the number and synchronize access; if
later you want to hold counters in a database for
persistence, you can change the private implementation of
the Singleton without changing the interface.

On the other hand, Singletons can also be stateless,
providing utility functions that need no more information
than their parameters. In that case, there is no need to
instantiate multiple objects that have no reason for their
existence, and so a Singleton is appropriate.

The Singleton should not be seen as way to implement global
variables in the Java programming language; rather, along
the lines of the factory design patterns, the Singleton lets
you encapsulate and control the creation process by making
sure that certain prerequisites are fulfilled or by creating
the object lazily on demand.

However, in certain situations, two or more Singletons can
mysteriously materialize, disrupting the very guarantees
that the Singleton is meant to provide. For example, if your
Singleton Frame is meant as a global user interface for your
application and two are created, your application will have
two Frames on the screen -- quite confusing for the user.
Further, if two counters are created where one was intended,
then clients requesting numbers will not get the desired
sequence 1, 2, 3... but rather a multiple sequence such as
1, 1, 2, 2, 3, 3, 3.... Additionally, if several instances
of a database-connection Singleton are created, you might
start receiving SQLExceptions complaining about "too many
database connections."

In this article, I'll describe those phenomena and how to
avoid them. After discussing how to implement the Singleton,
I'll go over the sometimes surprising causes for the
phenomena one by one, showing you how they occur and how you
can avoid making those mistakes. I hope that in the process
you will learn about classloading, multithreading,
distributed systems, Design Patterns, and other interesting
topics, as I did.

Is This Answer Correct ?    8 Yes 0 No

Answer / kamlesh

SIngleton class is class which instantiate only once.means
suppose you are using JDBC/HIBERNATE in this don't need to
create the multiple instance for
Connection/SessionFactory , so for this we are using this
Singelton design pattern to create the instance for this.
and also with the help of static reference.

Is This Answer Correct ?    12 Yes 5 No

Answer / harinath reddy.n

Instead of creating a separate object for every requirement we can create a single object and we can reuse the same object for every requirement

Is This Answer Correct ?    1 Yes 0 No

Post New Answer

More Core Java Interview Questions

class A { private int i; } class B extends A { private int i; } if I create an object of B class what will be the memory of that object.

2 Answers   Yamaha,

What is ResourceBundle class?

1 Answers  

what is an virtual function

2 Answers   TCS,

why string constant pool in java

2 Answers   TCS,

This is my code i have a doubt class ab implements a,b { public void add() { System.out.println("Hi") } } interface a { public void add(); } interface b { public void add(); } in this code i have two interface implemented in the class has same method.just i want to know which method of interface implemented in the class. interface a or interface b? confused me .

3 Answers  

What is Runtime class and its purpose?

2 Answers  

What collections you have worked on? Internal working of Hashmap?

0 Answers   Bravura Solutions,

What will happen if we write code like: try{}catch(exception e)catch(IOException i)

6 Answers   TCS, CTS,

How Vector class is synchronized,How to build user defined class as synchronized?

0 Answers  

how to compile jsp?

4 Answers   Logica CMG,

How to deprecate a method? Show it with proper example. Plz give the answer of this.Thanx in advance. mail me: tanzeem.akhtar@gmail.com

2 Answers  

if two references are having same hash codes,is that means those are refering to same object?

5 Answers   CTS,