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
What is native method in java?
What is outofmemoryerror in java?
What is final modifier?
What happens if we don’t define serial version uid?
What are three advantages of using functions?
What type of variable is gender?
What is a container in a gui?
Explain different ways of creating a thread?
Can we have any code between try and finally blocks?
What are streams?
How do you know if a value is nan?
Explain about the select method with an example?
What is the flag in java?
What does this () mean in java?
Is java 1.7 the same as java 7?