File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes cloning Singleton object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "cloning Singleton object" Watch "cloning Singleton object" New topic
Author

cloning Singleton object

shashanka jena
Greenhorn

Joined: Aug 23, 2006
Posts: 10
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


Thanks and Regards<br />shashanka Jena
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

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 ]

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Thomas Thrien
Greenhorn

Joined: Mar 05, 2008
Posts: 5
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().


Gl�ck ist die statistische Notwendigkeit des positiv empfundenen Zufalls.
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
Post the error message / stacktrace


Manuel Leiria<br /> <br />--------------<br />Peace cannot be kept by force; it can only be achieved by understanding. <br /> Albert Einstein
Syam Sathyan George
Ranch Hand

Joined: Apr 26, 2006
Posts: 64
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.


Syam.Sathyan.George
(My boss is a Jewish carpenter)
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

What is that exception? Can you post your cloe() method?
Manuel Leiria
Ranch Hand

Joined: Jul 13, 2007
Posts: 171
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

Joined: Apr 26, 2006
Posts: 64
I understand deep cloning is another topic by itself.
Need to see the stack trace, to comment
Bill Shirley
Ranch Hand

Joined: Nov 08, 2007
Posts: 457
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?)


Bill Shirley - bshirley - frazerbilt.com
if (Posts < 30) you.read( JavaRanchFAQ);
Vishal Matere
Ranch Hand

Joined: Jan 22, 2008
Posts: 81
2 ways of implementing clone.

1.


2.


HTH
V
[ May 07, 2008: Message edited by: Vishal Matere ]

SCJP <br />SCWCD <br />SCBCD <br />SCEA-1
Vishal Matere
Ranch Hand

Joined: Jan 22, 2008
Posts: 81
As you can see here , we never create duplicate objects of singleton class.
Only one object exists as per definition of Singleton pattern.

V
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: cloning Singleton object