aspose file tools*
The moose likes Beginning Java and the fly likes Why Object.clone() ? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why Object.clone() ?" Watch "Why Object.clone() ?" New topic
Author

Why Object.clone() ?

henry akoma
Greenhorn

Joined: May 17, 2005
Posts: 21
guys,
please i am yet to understand the purpose of the clone method in Object. why clone an object(say, object1) when u can just use the same object to achieve all ur purposes.
see the code below:


the method g and g2 both give output "dog", so why bother clone test, since object test can be called in other methods to achieve the same purpose.

please i need some clarification
thank u.
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3742
    
  16
If you are going to post code at least make sure that it compiles.
People might then be able to understand the point you are trying to make.


Joanne
Manuel Moons
Ranch Hand

Joined: Mar 05, 2002
Posts: 229
In some cases you might want to copy an object because you do not want the original object to be manipulated or just because you need more copies of that object.

After cloning an object those two object do not refer to the same space in memory (so object1 != object2 but object1.equals(object2)). They might represent the same data but when one is altered the other one does not follow.
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Take this snipet for example:

What will the output be? This should illustrate the usefulness of the clone() method when it is implemented correctly. (I apologize for any compiler errors; I didn't check.)

Does this help? If you don't understand why the output is the way it is, then please come back with more questions.

Regards,

Layne


Java API Documentation
The Java Tutorial
Ariel Ortiz
Ranch Hand

Joined: May 14, 2004
Posts: 121
When implementing the clone method, you must take the following into acount (from the Object class API):
By convention, the returned object should be obtained by calling super.clone. If a class and all of its superclasses (except Object) obey this convention, it will be the case that x.clone().getClass() == x.getClass().


Layne's code uses the new operator instead, thus any subclasses of the Animal class might not work correctly.

...Ariel
Layne Lund
Ranch Hand

Joined: Dec 06, 2001
Posts: 3061
Originally posted by Ariel Ortiz:
When implementing the clone method, you must take the following into acount (from the Object class API):


Layne's code uses the new operator instead, thus any subclasses of the Animal class might not work correctly.

...Ariel


Thank you for clarifying this. I apologize for the error in my code.

Layne
Ariel Ortiz
Ranch Hand

Joined: May 14, 2004
Posts: 121
Just for those who were wondering, a better way to implement the clone method in Layne's code above would be:


...Ariel
Rick O'Shay
Ranch Hand

Joined: Sep 19, 2004
Posts: 531
>> Why clone an object when you can just use the same object to achieve your purpose.

In that case you would not clone the object. However, what if your purpose is to create a copy that is independent of the original? Consider an object that serves as a default command. You want to keep that as-is and hand out copies.

Clone might be a good solution, but only conceptually. In fact you should not use clone because it serves no useful purpose. The idea is that you copy your object's state and its super class, and its super class and so on. There's no guarantee that will be implemented correctly or that CloneNotSupported will be thrown when necessary. Joshua Bloch, author of Effective Java, addresses all of the problems. He left out that it breaks entirely with Generics because that wasn't available at the time.

C++ had the notion of a copy constructor and you can do the same thing in Java to a degree:

class Bean {
public Bean( final Bean copy ) ....
A Kumar
Ranch Hand

Joined: Jul 04, 2004
Posts: 979
Hi,


By convention, the returned object should be obtained by calling super.clone........


I am stuck at this statement ....Can u explain the statement in detail with the above example..of Layne

If super.clone is called are we not caling the superclass Object clone method..?

Tx
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Why Object.clone() ?