This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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.
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 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
posted
0
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.
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.
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.
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?
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.
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
posted
0
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
posted
0
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.
subject: problem in clone() while making singleton