aspose file tools*
The moose likes Java in General and the fly likes Avoid cloning of singleton object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Avoid cloning of singleton object" Watch "Avoid cloning of singleton object" New topic
Author

Avoid cloning of singleton object

Juni Panda
Greenhorn

Joined: Mar 18, 2012
Posts: 5
Hi,

Can anybody clear my confusion on the object cloning concept.
I wrote a singleton class ... then somebody told me with cloning I can get one more object. Then I started searching over the internet.

I read many articles saying to avoid cloning we need to override the Object clone() method.. but what is the need of it..

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."

Calling the clone method on an object, will result in throwing an exception at run time.

Hence, when I am writing a singleton class and not implementing Cloneable interface, there is no chance of anyone cloning this singleton object. Even though clone method comes from Object super class..
I guess, any object whose class implements this Cloneable interface is eligible for cloning.. Please clear me if I am wrong..

Thanks
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

Hence, when I am writing a singleton class and not implementing Cloneable interface, there is no chance of anyone cloning this singleton object. Even though it comes from Object super class..

There are some ways by which you can clone it, one of them being


Here are few links so that you can find more about singletons

Link 1
Link 2

Hope it clears your doubt.


Thanks & Regards, Sumeet
SCJP 1.4, SCWCD 5, LinkedIn Profile
rohit chavan
Ranch Hand

Joined: Oct 08, 2010
Posts: 132

Juni Panda wrote:Hi,

Can anybody clear my confusion on the singleton object cloning concept.
I read many articles saying to avoid cloning we need to override the Object clone() method..
But 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."

Hence, when I am writing a singleton class and not implementing Cloneable interface, there is no chance of anyone cloning this singleton object. Even though it comes from Object super class..
I guess, any object whose class implements this Cloneable interface is eligible for cloning.. Please clear me if I am wrong..

Thanks


If you want to create a Singleton Class, which should not be cloned by any other Class, simply DO OVERRIDE clonable interface's clone() method with a blank implementation.[Edited the post after referring Sumit's post above]
Hope this clears your doubt, else please reframe the question.
Juni Panda
Greenhorn

Joined: Mar 18, 2012
Posts: 5
But when I am writing a singleton class and not implementing Cloneable interface, there is no chance of anyone cloning this singleton object. right?
Then why to override the clone method?
Sumit Patil
Ranch Hand

Joined: May 25, 2009
Posts: 296

Please go through the links provided above.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Avoid cloning of singleton object