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 Anyone know the model / questions of the Federal bank sample questions for the post of Specialist Officers - Programmers. Please post if anyone have..

1037


which one is more efficient int x; 1. if(x==null) 2.if(null==x) state which one either 1 or 2?

5544


hr interview how many minutes asking question

793


how to prepare for IT Officers Interview in Banks

691


How to provide security in java

923


please send me hr interview questions in it industry

707


What is the difference between Java1.4 and Java1.5

902


What is procedure overloading?

887


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

923


how to write a program for sending mails between client and server

774


How to add and remove nodes in Jtree?

1331


Q1.A. Write note on “The class path Environment Variable”? B. Which are different kinds of source code? Q2.A. How to create an interface? B. Why convert an applet to an application? Q3.A. How to use Media tracker Class. B. How to use string tokenizer class. Q4 A. Explain the overview of UDP messaging. B. Difference between SQL Exception class and SQL Warning class. Q5. A. How to create com object in Java? B. Write short notes on “The properties class” Q6. A. When object is created and destroyed? B. Explain the JDB in depth & command line. C. Write short notes on Web Sites.

5465


What are the differences between Java 1.0 and Java 2.0?

811


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)

527


Why Set interface contains unique elements, what internally implemented for this so that it contains unique elements?

5853