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
What is the use of protected in java?
What is the difference between yield() and sleep()?
What are peerless components in java programming?
What is concurrent hashmap and its features?
What is the use of string and stringbuffer?
What are the important methods of java exception class?
What are anonymous inner classes?
What is a singleton in genetics?
How will you reverse a link list without using recursion?
What is integer parseint?
Can constructor be synchronized?
What is classname class in java?
What is java used for on a computer?
Difference between method overloading and overriding.
What are the concepts of 'OOPS'?