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 do you understand by synchronization? Why is it important?
What are the different types of collections views being provided by the map interface? : java collections
What is an algorithm in java collection framework? : java collections
How is hashset maintained in memory by java ?
What is the use of the list interface in the java collection? : java collections
What are the uses of the set interfaces in the java collections? : java collections
please mail me the interview question based on java/j2ee
What are the queues in the java collection framework? : java collections
Which java collection class can be used to maintain the entries in the order in which they were last accessed?
What are the differences between the java collection and the java list? : java collections
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
HOW MUCH PERCENTAGE WE DEDUCT FOR OUR SUPPLIERS OR OTHER PROFESSIONALS
my interviewer asked me what technical specification you used how to answer that question
How do I find jre path in windows?
What is the difference between comparable and comparator in java.util pkg?