This week's book giveaway is in the Programmer Certification forum.
We're giving away four copies of OCP Oracle Certified Professional Java SE 21 Developer (Exam 1Z0-830) Java SE 17 Developer (Exam 1Z0-829) Programmer’s Guide and have Khalid Mughal and Vasily Strelnikov on-line!
See this thread for details.
  • 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
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

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: 22849
132
Eclipse IDE Spring 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: 22849
132
Eclipse IDE Spring 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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic