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
In java, how many ways you can take input from the console?
What is the difference between Error, defect,fault, failure and mistake?
When should a function throw an exception?
How to create packages in java?
What is an eror in java?
Is empty string in java?
What is a java string?
Can we overload the constructors?
Can a java program have 2 main methods?
What is the static keyword?
What are exceptions
Tell me how many ways are there to initialise an integer with a constant.
What state does a thread enter when it terminates its processing in java programming?
Can we override tostring method in java?
what is difference between equals and ==?