File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

problem in clone() while making singleton

 
Rakesh s Verma
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Singleton1 is not implemented Clonable interface
 
Rakesh s Verma
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why would you want to clone a singleton? Doesn't that defeat the purpose of having a singleton?
 
sachin verma
Ranch Hand
Posts: 177
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it is a matter of confusion for me as well. can anybody elaborate it..
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4660
5
Linux Mac OS X VI Editor
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you implement clone() on a Singleton class, don't you have to rename it to DoubleTon?
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Or TwoTon.
 
Rakesh s Verma
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Rakesh s Verma
Greenhorn
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10444
227
IntelliJ IDE Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic