• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem in clone() while making singleton

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 5575
Eclipse IDE Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Singleton1 is not implemented Clonable interface
 
Rakesh s Verma
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Windows XP Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why would you want to clone a singleton? Doesn't that defeat the purpose of having a singleton?
 
Ranch Hand
Posts: 177
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
it is a matter of confusion for me as well. can anybody elaborate it..
 
David Newton
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 4803
7
Mac OS X VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Or TwoTon.
 
Rakesh s Verma
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
reply
    Bookmark Topic Watch Topic
  • New Topic