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
How to solve the problem of generating the unique hash keys with hash function?
Where we write javascript code in html page?
What is the difference between delete and delete[]
Why wait and notify methods are declared in object class?
what is collatration?
How to retrieve data from database in java using arraylist?
What is mnemonic code?
how we can make a read-only class in java?
How are the elements of a gridbaglayout organized in java programming?
What are the data types supported by java? What is autoboxing and unboxing?
What is port number in java?
Why do we create threads in java?
Can an interface implement another interface?
Can a private method of a superclass be declared within a subclass?
Explain about field hiding in java?