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 variable and example?
How do I find and replace in word?
What is purpose of applet programming?
What is the difference between java applets and applications?
What is the difference between C++ and Java and your preferences?
What is numel matlab?
How are variables stored?
What is array size in java?
What is the gregoriancalendar class in java programming?
Name component subclasses that support painting in java programming?
How do you define a parameter?
Is null false in java?
How do you sort objects in java?
Why 1 is not a prime number?
define the terminology association.