wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Doubt regarding clone method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Doubt regarding clone method" Watch "Doubt regarding clone method" New topic
Author

Doubt regarding clone method

abin joy
Ranch Hand

Joined: Jul 29, 2008
Posts: 35
what does the clone method in the Object class dO?
In which context we use the clone function?
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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 ]

[LEARNING bLOG] | [Freelance Web Designer] | [and "Rohan" is part of my surname]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

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:


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Sagar Rohankar
Ranch Hand

Joined: Feb 19, 2008
Posts: 2902
    
    1

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

Joined: Oct 27, 2005
Posts: 19653
    
  18

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Doubt regarding clone method
 
Similar Threads
WA #1.....word association
Object class clone() method
Prototype Pattern
Problem with declared type vs actual type
clone() method