• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

Avoid cloning of singleton object

 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 296
Eclipse IDE Firefox Browser Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 133
Hibernate Oracle Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please go through the links provided above.
 
Ranch Hand
Posts: 230
IntelliJ IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
Marshal
Posts: 80235
424
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Santosh Kumar Nayak
Ranch Hand
Posts: 230
IntelliJ IDE Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes Enum rocks
 
This. Exactly this. This is what my therapist has been talking about. And now with a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic