1) write a program that singleton objects returns two instances?

Give me answer

Showing Answers 1 - 1 of 1 Answers

dilipiyer

  • Jan 28th, 2006
 

We can use clone() method to create another instance of a singleton class.

Example:
SingletonObjectClass obj =
SingletonObjectClass.createSingletonObject();

SingletonObjectClass clone =
(SingletonObjectClass) obj.clone();

java.lang.Object class has a clone method and all class are inherited from it.To avoid creation of another object in our code we should override the clone() function and throw CloneNotSupportedException.

Example:
public Object clone() throws CloneNotSupportedException {
    throw new CloneNotSupportedException();
}

This will ensure that no new object is created.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions