aspose file tools*
The moose likes Java in General and the fly likes problem in clone() while making singleton Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "problem in clone() while making singleton" Watch "problem in clone() while making singleton" New topic
Author

problem in clone() while making singleton

Rakesh s Verma
Greenhorn

Joined: Dec 29, 2009
Posts: 8
HI Ranchers,
I am facing a difficulty while trying to create a clone(just to test) of singleton(Singleton1) class. I am getting an error of "clone() has protected access in java.lang.Object".
And I don't understand this error.
I know by default Object is a super class of every one that we create(directly or indirectly).
And hence when I test instanceof , it result in a true(as expected).
So my Singleton is also a subclass of Object class. Now I try to use clone() method of Object class which has protected access(that means either through inheritance or always with the object of inheriting class).
And it will be accessible, as my class is a subclass of Object class and protected members can be accessed in subclass regardless of the directory structure.

So why does it not work

Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Singleton1 is not implemented Clonable interface
Rakesh s Verma
Greenhorn

Joined: Dec 29, 2009
Posts: 8
seetharaman venkatasamy wrote:Singleton1 is not implemented Clonable interface


Is that mandatory?
We can always access other methods of Object class without that kind of obligation!!!
So why in the case of clone() method ?
And the problem is persistent even by implementing Cloneable interface
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Rakesh s Verma wrote:
seetharaman venkatasamy wrote:Singleton1 is not implemented Clonable interface


Is that mandatory?

Yes

Rakesh s Verma wrote:
We can always access other methods of Object class without that kind of obligation!!!
So why in the case of clone() method ?


Overriding clone is not a normal Operation.you should be careful[i do suggest you to use Copy Constructor]
Rakesh s Verma
Greenhorn

Joined: Dec 29, 2009
Posts: 8
And the problem is persistent even by implementing Cloneable interface if i do not override the clone() method in Singleton1 which I should as written on Cloneable interface.
But by simple OO it must work. And if it is not working there must be a logic behind it.
but when I checked the code in Object class, I found this.

protected native Object clone()
throws CloneNotSupportedException;
but the logic is not defined there and the cone() methos is declared native also. So where might I find the exact reason for this happening.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Why would you want to clone a singleton? Doesn't that defeat the purpose of having a singleton?
sachin verma
Ranch Hand

Joined: Mar 25, 2008
Posts: 177
it is a matter of confusion for me as well. can anybody elaborate it..


SCJP || SCWCD

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

sachin verma wrote:it is a matter of confusion for me as well. can anybody elaborate it..

Hmm, that's not what this message said the first time--before it said that something along the lines of "as above, I said it was for learning purposes" or something like that.

Weird.
Pat Farrell
Rancher

Joined: Aug 11, 2007
Posts: 4634
    
    5

If you implement clone() on a Singleton class, don't you have to rename it to DoubleTon?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Or TwoTon.
Rakesh s Verma
Greenhorn

Joined: Dec 29, 2009
Posts: 8
As suggested by you guys , I am changing the name as it does not work as it should be.
But I had my own reason as I wanted to make a singleton class.
But as far as I know my code still does not create a singleton that I am trying to.

So can anybody tell me how I can create the singleton of TwoTon class in TwoTonMain class.
when I make line e1, a comment it works fine but not otherwise.


Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9315
    
109

Rakesh s Verma wrote:As suggested by you guys , I am changing the name as it does not work as it should be.

No one was serious when they suggested TwoTon or DoubleTon
Rakesh s Verma wrote:
But I had my own reason as I wanted to make a singleton class.
But as far as I know my code still does not create a singleton that I am trying to.


Creating a singleton doesn't require any use of clone(). You haven't yet told us why you are after the clone method?

[My Blog] [JavaRanch Journal]
Rakesh s Verma
Greenhorn

Joined: Dec 29, 2009
Posts: 8
Jaikiran Pai wrote:

Creating a singleton doesn't require any use of clone(). You haven't yet told us why you are after the clone method?

It could be interesting then!!
Ok Read this heading on the link Preventing thread problems with your singleton. where it is written about cloning.
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9315
    
109

All it says is:
So, to be absolutely positively 100% certain that a singleton really is a singleton, we must add a clone() method of our own, and throw a CloneNotSupportedException if anyone dares try!


So, something like:



That article also says:

The restriction on the singleton is that there can be only one instance of a singleton created by the Java Virtual Machine (JVM) - by prevent direct instantiation we can ensure that developers don't create a second copy.

That's not really the case with singletons. If there are multiple classloaders, then there is no guarantee that there will be only one instance of this "singleton" in the JVM.

By the way, i would recommend that you search the forum here for "singleton" and you will find a lot of threads discussing about singletons and you will understand more about them.

Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9315
    
109

By the way, that article mentions only a few ways of breaking a singleton and ways to prevent that. But there are other ways too, which can easily break the singleton semantics. I am sure a search for "singleton" in this forum will provide you enough information.
Rakesh s Verma
Greenhorn

Joined: Dec 29, 2009
Posts: 8
Thanks a lot for your time and reply.

Jaikiran Pai wrote:By the way, that article mentions only a few ways of breaking a singleton and ways to prevent that. But there are other ways too, which can easily break the singleton semantics. I am sure a search for "singleton" in this forum will provide you enough information.


I am on it

Kaustav Ganguly
Greenhorn

Joined: Dec 14, 2009
Posts: 28
Rakesh s Verma wrote:
And it will be accessible, as my class is a subclass of Object class and protected members can be accessed in subclass regardless of the directory structure.
:


But here you are trying to access the superclass method from outside the subclass. In this case your subclass is Singleton1 and you are trying to access the super class method from Singleton2.

Protected access modifier says you can access the superclass methods from within the subclass, not outside of it, so what it means is you can access the superclass method from a method of the subclass. In your case you are trying to call the protected superclass method but from a method in Singleton2 (which is not the subclass).
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in clone() while making singleton
 
Similar Threads
Java class
Singleton Class
hashCode
Ordering of AlphaNumeric No. in ascending Order
Executing Singleton class with Custom Class Loaders