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
how to convert Java project into Maven ?
what is deadlock? : Java thread
What is the maximum size of list in java?
What is the purpose of the return statement?
Can java list be null?
What is the integer of 16?
What is the difference between serial and throughput garbage collector?
What is the java idl system?
What ide should I use for java?
What is a java list?
What is the main use of java?
What is the difference between a constructor and a method?
What is javac used for?
What are the java ide's? Explain
What is boolean keyword in java?