• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can I clone my class like this?

 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jagdeep Sharma
Ranch Hand
Posts: 121
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wrong, It's like normal method. It will work but it will not return clone instead it will return new object.
 
Jason Cone
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49823
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Ritchie, I will make a search.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49823
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Jason Cone
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 47
Java Linux Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic