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 loop in java?
What is the difference between numeric and integer?
Which collections are thread safe in java?
Explain about interrupt() method of thread class ?
What is finally and finalize in java?
How many bits is a float?
Is singleton class immutable?
What is singletonlist in java?
Can private method static?
How to restrict a member of a class from inheriting by its sub classes?
What is method overloading in JAVA? Why is it not present in C ?
Why singleton class is used in java?
what are the methods in object?
Define an applet in java?
What is stringreader?