Win a copy of Rust Web Development this week in the Other Languages forum!
  • 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:
  • Tim Cooke
  • Campbell Ritchie
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Doubt regarding clone method

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
what does the clone method in the Object class dO?
In which context we use the clone function?
 
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I want you to read its docs first !

clone method , as far as I know , returns an different object from heap of same type with may be same values .

When you do something like this,



This two objects are referring to same object on heap !

But when you do something like this ,



Here obj2 is totally different object of type 'CloneableObject'..

I know this much only , fellow rancher ll explain us in detail !
[ September 06, 2008: Message edited by: Sagar Rohankar ]
 
Sheriff
Posts: 22567
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Sagar Rohankar:
I want you to read its docs first !

...

Here obj2 is totally different object of type 'CloneableObject'..


Have you read your own link? It clearly says that although it is usual that obj1 and obj2 are different objects, they can be the same.

As far as I know, Object.clone() creates a new instance of the same class and makes shallow copies of all fields. In code, it's something similar to this:

That's why, if you don't want to share the objects, you will need to copy those:
 
Sagar Rohankar
Ranch Hand
Posts: 2908
1
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Prime:

Have you read your own link? It clearly says that although it is usual that obj1 and obj2 are different objects, they can be the same.



You may misinterpret it ! At the start of my thread I clearly wrote this ,

clone method , as far as I know , returns an different object from heap of same type with may be same values .



Surely the object is different but they may or may not have same values or pass the Object#equal() test ! (Thats totally depends upon the implementation of clone() method )
 
Rob Spoor
Sheriff
Posts: 22567
122
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
But they don't need to be different. clone() is very much allowed to return "this":

The general intent is that, for any object x, the expression:

will be true, and that the expression:

will be true, but these are not absolute requirements. While it is typically the case that:

will be true, this is not an absolute requirement.


So x.clone() == x is most definitely allowed - highly unusual, but allowed.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic