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 final class modifier?
Why do we need wrapper class?
Do you know how to reverse string in java?
Explain the difference between treeset and treemap in java?
What is the java idl system?
Explain the scope of a variable.
How many types of operators are there?
What is a “stateless” protocol ?
What is scope & storage allocation of static, local and register variables? Explain with an example.
Why we use methods in java?
What classes of exceptions may be thrown by a throw statement?
What is immutable in java?
What is number data type?
Can we call virtual funciton in a constructor ?
What are unchecked exceptions in java?