Answer Posted / shaik baji
Singleton Example:
==================
public class Singleton
{
private static Singleton singleton;
private Singleton()
{
}
public static Singleton getInstance()
{
if(singleton==null)
{
singleton = new Singleton();
return singleton;
}
else
{
return singleton;
}
}
}
Accessing Singleton instance
============================
public class STDemo
{
public static void main(String[] args)
{
Singleton obj1= Singleton.getInstance();
System.out.println(obj1.hashCode());
Singleton obj2= Singleton.getInstance();
System.out.println(obj2.hashCode());
Singleton obj3= Singleton.getInstance();
System.out.println(obj3.hashCode());
}
}
| Is This Answer Correct ? | 0 Yes | 1 No |
Post New Answer View All Answers
Can we serialize arraylist in java?
How to print an arraylist in java?
Why is java multithreaded?
How can u increase the heap size in the memory?
Explain about java sdk?
What’s a deadlock?
Can memory leak happen java?
Why does java not support operator overloading?
What is a platform?
Can we clone singleton class in java?
Who is the owner of java?
What do you mean by formatting?
What is the difference between exception and error in java?
What is field name?
How can we find the sum of two linked lists using stack in java?