• 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

Can I clone my class like this?

 
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 121
IBM DB2 Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ritchie, I will make a search.
 
Campbell Ritchie
Marshal
Posts: 79180
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're welcome
 
Jason Cone
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.)
 
Ranch Hand
Posts: 47
Oracle Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Blueberry pie is best when it is firm and you can hold in your hand. Smell it. And smell this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic