What is singleton class?

Answer Posted / 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

Post New Answer       View All Answers

Please Help Members By Posting Answers For Below Questions

Hi.... I applied for the post of scientific officer/Engineer-SB(Programmer).Please post the syllabus and sample papers.


How to provide security in java


what is the use of bean managed and container managed with example?


Can I overload to string method


java Technical questions asked by JPMC


which pattern is default in scanner package?


explain the concept of virtual method invocation in polymorphism in detail?


How to connect to a remote database using Applet?


How to solve the problem of generating the unique hash keys with hash function?


For ease of programming you can consider the maze as a 2D array with colors represented by below integer and characters (in capital letters). • B - Black • W -White • G- Green • R- Red R B W B W W W W W W B W B B W W W W W W W B W B W W W B W W W W B B W W W B W W W B W W B B B B W B W B W W B W W W B W W W B B B W W B W W W B W W B W B W W W B W B W W W W B B W W W W B W W W W W G Shortest Route Problem: • Solution that finds the shortest Route between Red and Green  White will have 1 Weight.  Red and Green carry no weights.  Shortest path is the path with less weight when you add up the weights in the path.


whatis Home interface and Remoteinterface? with example?


Say you want to store the information about a number of pets in an array. Typical information that you could store for each pet (where relevant) would be • Breed of animal • Animal's name • Its birth date • Its sex • Whether it has been sterilised or not • When it is due for its next inoculation • When it last had its wings clipped For each type of pet (eg. dog, cat or bird) you would typically define a class to hold the relevant data. Note: You do not need to implement these classes. Just answer the following questions. 3.1.1 What would be the advantage of creating a superclass (eg. Pet) and declaring an array of Pet objects over simply using an array of Objects, storing each of the instances of the different pet classes (eg. Dog, Cat or Bird) in it? 3.1.2 Would you define Pet as a class or as an interface? Why? (2) (2)


what is ststic with example


why using interface interface ?


what are the disadvantages of indexes in oracle?