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
Are generics important java?
How do you define a singleton class?
What is diamond operator in java?
What is the purpose of abstract class?
What is java jit compilers?
What is difference between length and length() method in java ?
What is meant by design patterns?
Is an integer an object?
what are synchronized methods and synchronized statements? : Java thread
How many bits are used to represent unicode, ascii, utf-16, and utf-8 characters?
What is an immutable class?
What is wrapper class example?
Where will it be used?
Which variable is the independent variable?
what is interface in java? Explain