What happens if i clone a singleton object




















The class Object does not itself implement the interface Cloneable, so calling the clone method on an object whose class is Object will result in throwing an exception at run time. So you have to explicitly declare that your class implements Cloneable.

As long as you don't do it, there is no public clone method on your instance. But you wouldn't do that for a singleton since this would render your class design singleton useless.

If you did not declared the singleton class final and extends it with another class which an instance of would call super. If you would explicitlly declare that your singleton class implements Cloneable according to the Java-Doc this:.

Thus, this method performs a "shallow copy" of this object, not a "deep copy" operation. Therefore, it is not possible to clone an object merely by virtue of the fact that it implements this interface. Answering the question: Is it possible? Yes it is - but only if you allow it.

Is it intended? Also note: Beside the mentioned above by using reflection you could try to bypass the visibility restrictions of a singleton class to create further instances.

Second, clone do shallow copy. See here. Last, suggest to use enum for singleton, which is guarantee by VM. Stack Overflow for Teams — Collaborate and share knowledge with a private group.

Create a free Team What is Teams? Collectives on Stack Overflow. Learn more. Is it possible to shallow copy a singleton class object? Ask Question. Asked 3 years, 4 months ago. Active 3 years, 4 months ago. Viewed 1k times. Using the clone method, can we get many instances of a class which has been made singleton?

Prabhat Gaur. Prabhat Gaur Prabhat Gaur 1 1 silver badge 10 10 bronze badges. I don't think so, as the purpose of the singleton is to encapsulate itself, bearing its memory reference. Juni Panda. Optional 'thank-you' note:. Hi, Can anybody clear my confusion on the object cloning concept. I wrote a singleton class Then I started searching over the internet.

Followed this discussion - cloning singleton object As per the documentation , "Invoking Object's clone method on an instance that does not implement the Cloneable interface results in the exception CloneNotSupportedException being thrown.

Even though clone method comes from Object super class.. Sumit Patil. I like There are some ways by which you can clone it, one of them being Enter the email address associated with your account. We'll send a magic link to your inbox. Email Address. All Sign in options. Enter a Email Address. Choose your interests Get the latest news, expert insights and market research, sent straight to your inbox.

Newsletter Topics Select minimum 1 topic. Tags: Java. Is it possible to clone a singleton object. If we are able to clone a singleton object then what the purpose of using singleton. Hi, Actually speaking a singleton is supposed to have a single obj in a JVM.. Hi, I think we cant clone the singleton. That is the advantage of the singleton.

We can clone singleton object by using Object class clone method..



0コメント

  • 1000 / 1000