August 14, 2012

How to avoid the copy of a Singleton class, instance using clone...

1. Make your class final (so that no cloneable sub-class can be created)
2. In case of non-final class, Override the clone method of Object
class in your class and inside the body of the method just throw a
CloneNotSupportedException.

ex:
public final class MyClass
{
protected Object clone() throws CloneNotSupportedException
{
throw new CloneNotSupportedException();
}
}

No comments:

Post a Comment

I'm certainly not an expert, but I'll try my hardest to explain what I do know and research what I don't know.

My Favorite Site's List

#update below script more than 500 posts