Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Avoid cloning of singleton object

 
Juni Panda
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
rohit chavan
Ranch Hand
Posts: 132
Hibernate Java Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please go through the links provided above.
 
Santosh Kumar Nayak
Ranch Hand
Posts: 143
Eclipse IDE IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Campbell Ritchie
Sheriff
Pie
Posts: 48957
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have corrected the code tags for you.
You would appear to have a contradiction there, with implementing the Cloneable interface and throwing an Exception from clone().
I also think your attempt at checked locking will not work.

Anyway, everybody now knows the best way to implement singletons is to create an enum.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic