How to implement or use the singleton class in java?
Answer Posted / aashish r. wadhokar
Singleton is a design pattern as well as class.
when we define singleton class it means we can have only one
object of that class.
We cant create multiple objects of that class.
In singleton class, constructor is always private.
eg:
public class Singleton
{
private static Singleton singletonObject = null;
private Singleton()
{
System.out.println("Singleton object created!!!");
}
public static Singleton CreateInstance()
{
if(singletonObject == null)
{
singletonObject = new Singleton();
}
return singletonObject;
}
Now we can get object as:
Singleton ref = null;
ref = Singleton.CreateInstance();
| Is This Answer Correct ? | 6 Yes | 1 No |
Post New Answer View All Answers
What are the different types of classes implemented in the set interfaces? : java collections
What is deque in the java collections framework? : java collections
my interviewer asked me what technical specification you used how to answer that question
What are the types of java collections? : java collections
What are the interfaces in java collections? : java collections
What are the different types of collections views being provided by the map interface? : java collections
What is the difference between comparable and comparator in java.util pkg?
What are the queues in the java collection framework? : java collections
What are the types of interface used in the java collections? : java collections
HOW MUCH PERCENTAGE WE DEDUCT FOR OUR SUPPLIERS OR OTHER PROFESSIONALS
can u draw class/object diagram for ATM
Which java collection class can be used to maintain the entries in the order in which they were last accessed?
Hi frnds how to lock an user when he enter wrong credentials more than 3 time using java or j2ee tech take username and password in a bean no need to connect DB and validate give me some sample application code or links its urgent for me thanks in advance
who will give req's to u?. how they send req's to u? . what design documents contains?. when bugs raised on other developer code how to report to them?.(throgh mail or ........). how to retrive 100 recods from dao layer to presentation layer.using which collection?. what is sequence diagram.?.
what is mean by hasing and maping in java platform and advantage?