Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

cloning Singleton object

 
shashanka jena
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Can we clone Singleton object, if not why ?
Actually I am trying to clone Singleton object it is showing error, even after implementing the Cloneable interface I am getting same error.

Can anybody explain me the proper reason.

Thanks in Advance
Shashanka
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you clone a singleton? It should be possible (it would, of course, no longer be a singleton) but why do it?
[ May 07, 2008: Message edited by: Paul Sturrock ]
 
Thomas Thrien
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
First, the main feature of a Singleton is that it exists exactly once. So usually cloning does not make much sense. But as "once" is still relative, one can imagine scenarios where cloning of a Singleton is necessary.

But next: without the error message and the code for the Singleton class it is nearly impossible to determine the reason for the fail of clone().
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Post the error message / stacktrace
 
Syam Sathyan George
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anti pattern for sure.
But generally cloning an object is simple and painless, unless it does not override the clone() method and is throwing an exception inside it.
 
Paul Sturrock
Bartender
Posts: 10336
Eclipse IDE Hibernate Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is that exception? Can you post your cloe() method?
 
Manuel Leiria
Ranch Hand
Posts: 171
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Syam Sathyan:

But generally cloning an object is simple and painless,


It's not that simple! There's a lot of ins and a lot of outs that must be taken into account when cloning an object (for instance if the object contains fields that refer to mutable objects)
[ May 07, 2008: Message edited by: Manuel Leiria ]
 
Syam Sathyan George
Ranch Hand
Posts: 64
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand deep cloning is another topic by itself.
Need to see the stack trace, to comment
 
Bill Shirley
Ranch Hand
Posts: 457
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Of course, the Singleton design pattern and the Cloneable Java interface are at odds with one another.

Depending on what you mean by a cloneable singleton, and how you implemented the Singleton pattern in Java, it may be possible (if still undesirable).

(Should clone() return the singleton instance?)
 
Vishal Matere
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
2 ways of implementing clone.

1.


2.


HTH
V
[ May 07, 2008: Message edited by: Vishal Matere ]
 
Vishal Matere
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As you can see here , we never create duplicate objects of singleton class.
Only one object exists as per definition of Singleton pattern.

V
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic