aspose file tools*
The moose likes Java in General and the fly likes Can I clone my class like this? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can I clone my class like this?" Watch "Can I clone my class like this?" New topic
Author

Can I clone my class like this?

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Hi everybody, can I clone my class's object like this. What is the difference between the code in the commented part and which I have written. What I understood is the commented part calls the super.clone() method which returns an Object of the current class. Can I directly create a new object explicitly and return it instead using that code.

Is it right or wrong?

Thank you all in advance. Have a good day.


Love all, trust a few, do wrong to none.
Jagdeep Sharma
Ranch Hand

Joined: May 24, 2010
Posts: 121

Wrong, It's like normal method. It will work but it will not return clone instead it will return new object.
Jason Cone
Greenhorn

Joined: Jul 21, 2004
Posts: 25
It's wrong. Your clone method is not creating a clone, it's just creating a new instance (which will have the default values for all fields). A clone should be a field-by-field copy of the original object. Also, when you override the clone method of a non-final class you should be invoking super.clone() to get the object. If all of your fields are primitives or immutable objects, that's all you need to do. If you have fields that are mutable, you should start with an object returned by super.clone() and then make sure you set the state for the mutable fields.

Implementing Cloneable can be quite tricky.

In the case of your MyClone class, you have primitive fields without any mutable objects, so your clone method just need to return super.clone(). That will return a copy of the original object, including correctly set values for the primitive fields (i.e. int a and int b).
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

But anyhow, regardless of calling super.clone() or returning a new object, both times a new instance is created for the object. What is the difference exactly?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39861
    
  28
Search for Effective Java Joshua Bloch; that book has a description of the clone() method (and says it is not good design); there used to be a sample chapter on the internet free of charge, which included clone().
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Thank you Ritchie, I will make a search.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39861
    
  28
You're welcome
Jason Cone
Greenhorn

Joined: Jul 21, 2004
Posts: 25
chaitanya karthikk wrote:But anyhow, regardless of calling super.clone() or returning a new object, both times a new instance is created for the object. What is the difference exactly?

When you create a new instance, the values of its fields will be set to the "defaults." When you clone an object, the values of its fields should be set to the value of the original object. So:

using new: create a brand new object with default field values
using clone(): create a brand new object and set its field values to match the original

(I agree with tracking down Bloch's Effective Java. It's an excellent book. Note, however, that Effective Java assumes you're familiar and comfortable with the basics. It's not an introductory text.)
himanshu.harish agrawal
Ranch Hand

Joined: Oct 18, 2010
Posts: 47

Jason Cone wrote:If you have fields that are mutable, you should start with an object returned by super.clone() and then make sure you set the state for the mutable fields.


I think you mean to mention deep cloning by this, because this is what we need to do with mutable objects. But there is certainly a flip in it that the mutable object should also support cloning else you will land up with CloneNotSupportedException.

Thanks.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can I clone my class like this?