What is singleton class?

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


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



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

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

1636


Where we write javascript code in html page?

750


What is the difference between delete and delete[]

836


Why wait and notify methods are declared in object class?

778


what is collatration?

2977






How to retrieve data from database in java using arraylist?

721


What is mnemonic code?

746


how we can make a read-only class in java?

722


How are the elements of a gridbaglayout organized in java programming?

716


What are the data types supported by java? What is autoboxing and unboxing?

741


What is port number in java?

770


Why do we create threads in java?

768


Can an interface implement another interface?

751


Can a private method of a superclass be declared within a subclass?

720


Explain about field hiding in java?

688