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
Which is bigger double or float?
When do we need to use internal iteration? When do we need to use external iteration?
How many types of string data types are there?
what is the constructor and how many types of constructors are used in java?
What is static variable with example?
What is mnemonic in assembly language?
What is nested class?
What is a singleton puppy?
What is difference between java and java ee?
Why does the integer quotient -0/3 yield 0, but the double quotient -0.0/3.0 yields – 0.0?
What advantage do java's layout managers provide over traditional windowing systems?
Does google use java?
Why is multithreading important?
What do you understand by an io stream?
How does compareto method work?