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
Give an example of call be reference significance.
How many JVMs can run on a single machine and what is the meaning of Just-In-Time (JIT) compiler?
Why 1 is not a prime number?
I want to control database connections in my program and want that only one thread should be able to make database connection at a time. How can I implement this logic?
Does apple use java?
How do you override a variable in java?
How do you declare an array that will hold more than 64KB of data?
when should you use stringbuilder class in a program?
Why parsing is done?
Why singleton class is used in java?
What is difference between == equals () and compareto () method?
What is externalizable?
What is static keyword in java?
How to sort an array from smallest to largest java?
Which is better ascii or unicode?