The Java-Doc says about Object.clone()
:
Creates and returns a copy of this object. The precise meaning of
"copy" may depend on the class of the object.
It depends on your implementation of the clone()
method what kind of copy or clone you get. But the Java-Doc says further:
By convention, the object returned by this method should be
independent of this object (which is being cloned).
Following this convention a clone would be another independent instance of something that was a singleton instance before.
Going on with the Java-Doc:
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.clone()
this will throw the mentioned CloneNotSupportedException
.
If you would explicitlly declare that your singleton class implements Cloneable
according to the Java-Doc this:
creates a new instance of the class of this object and initializes all
its fields with exactly the contents of the corresponding fields of
this object, as if by assignment; the contents of the fields are not
themselves cloned. Thus, this method performs a "shallow copy" of this
object, not a "deep copy" operation.
To get a proper clone the Java-Doc of Cloneable
says:
... classes that implement this interface should override Object.clone ... Therefore, it is not possible to clone an object merely by virtue of the fact that it implements this interface.
So you really have to do it explicit.
Answering the question:
Is it possible? Yes it is - but only if you allow it.
Is it intended? No.
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.