Answer Posted / dara
Ensure a class has only one instance, and provide a global
point of access to it. Singletons maintain a static
reference to the sole singleton instance and return a
reference to that instance from a static instance() method.
Example:
========
public class MyClassSingleton {
private static MyClassSingleton instance;
//Constructor must be protected or private to perevent
creating new object
protected MyClassSingleton() {
}
//could be synchronized
public static MyClassSingletongetInstance() {
if (instance==null)
instance = new MyClassSingleton()
return instance;
}
public void method1(){
System.out.println("hello singleton");
}
}//end of class
| Is This Answer Correct ? | 1 Yes | 1 No |
Post New Answer View All Answers
What is the difference between applet and application?
Why map is used in java?
What is the importance of finally block in exception handling?
What is variable length arguments in java?
What language is java written?
How do you sort in ascending order in java?
How to create a custom exception?
How do you use nextline in java?
What is passing parameters in java?
Why string is a class?
What is keyword and identifier?
what do you mean by marker interface in java?
Explain some best practices you would apply while using collection in java?
What is function overriding and overloading in java?
How to Sort Strings which are given in List and display in ascending order without using java api.