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
Hi Friends, i have searched in google but not clear. can you give bank example with synchronized keyword
What are the different types of classes implemented in the set interfaces? : java collections
What is the enumerator of the java collection framework? : java collections
What is an algorithm in java collection framework? : java collections
Which interface does java.util.hashtable implement?
What are the different types of ways where you can iterate over a list? : java collections
please mail me the interview question based on java/j2ee
In hyderabad, which s/w training center is best for java, other than corejava what r the new tools to learn in java,which tool is best & have current requirement,pls give me information about java to learn ?
what is mean by hasing and maping in java platform and advantage?
What are the types of java collections? : java collections
What is deque in the java collections framework? : java collections
What are the interfaces in java collections? : java collections
What are the classes in the java collection framework? : java collections
What is the use of the list interface in the java collection? : java collections
What are the main classes of the list interfaces? : java collections