GeeCON Prague 2014*
The moose likes Beginning Java and the fly likes Is this a viable way to 'deep clone' objects? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is this a viable way to Watch "Is this a viable way to New topic
Author

Is this a viable way to 'deep clone' objects?

Peter Cimring
Greenhorn

Joined: Aug 13, 2008
Posts: 5
Hi

Could someone give me a quick 'sanity check' on the following utility method for 'deep cloning' objects?:



An example of the calling code:



I have not seen this used before, so I'm wondering what I have missed here.

Thanks
Pete
[ September 10, 2008: Message edited by: Peter Cimring ]
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234
IS this a deep cloning .... ? I dont think so ....

Even it is not a cloning.

You are just returning a reference to the same object and storing in other reference. Just one object is there with more than one reference.

Correct me if I am not correct
Peter Cimring
Greenhorn

Joined: Aug 13, 2008
Posts: 5
Well - that's the crux of my question: does the 'return' statement return a copy of the object being referenced, or just a copy of the reference variable?

Take this example:



Has 'my.foo' changed to "BAR"?

I admit that I may be totally confused - that's why I'm asking the question

Also, I realize that I could run some tests on my own - but I was hoping someone could provide some clarity on this - before I start 'chasing my own tail'
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3601
    
  15
Originally posted by Peter Cimring:
Well - that's the crux of my question: does the 'return' statement return a copy of the object being referenced, or just a copy of the reference variable?


Just a copy of the reference variable - in your original example after your cloneIt() method returns you will have two references (my and clone) pointing at the same object.
[ September 10, 2008: Message edited by: Joanne Neal ]

Joanne
Brij Garg
Ranch Hand

Joined: Apr 29, 2008
Posts: 234

does the 'return' statement return a copy of the object being referenced, or just a copy of the reference variable


copy of the reference varible is returned.


// my.foo == ???


In the case my.foo is still "bar" ... NOT "BAR"
because foo is a String variable which is mutable.


temp.toUpperCase();

above statement will create new object having value "BAR".
We still have a foo as a reference to object "bar";

If you do
temp=temp.toUpperCase();
Even then foo refers to "bar" and temp refers to new object "BAR".

I hope this will help you.
Peter Cimring
Greenhorn

Joined: Aug 13, 2008
Posts: 5
Thanks all for the responses - it's a bit clearer now. I still would like to clarify one last question:

Let's say we're talking about a more complex data member - not a String. I'll use a (random) example of a UserProfile object that is composed of:
- String values
- A 'CountryProfile' object.

Let's look at the following (rather pointless) code:



Does me.getCountryProfile().getCountry() return "USA"?
In other words, am I working with the original 'UserProfile.CountryProfile' object?

Many thanks
Pete
[ September 10, 2008: Message edited by: Peter Cimring ]
Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3601
    
  15
There are too many errors and unknown code there to know exactly what it will do. Why don't you complete the code and run it and then if it doesn't do what you expect come back and tell us what it is doing.

// In other words, am I working with the original 'UserProfile.CountryProfile' object?


You never create a CountryProfile object, so you are not working with the original or any other.
[ September 10, 2008: Message edited by: Joanne Neal ]
Peter Cimring
Greenhorn

Joined: Aug 13, 2008
Posts: 5
Sorry - I was rushing, so the example was a bit incorrect. I meant something like this:



If I understand the answers posted thus far, accessor methods will return a ref to the original data member - presuming that the data member is an object, not a primitive.

Since the whole idea of getters and setters is to prevent accidental / unauthorized change to the object's data member, I always had a (vaguely formed) notion that the 'getter' would return a copy (clone) of the object - in keeping with the idea that you could only change the data member via the 'setter'.

I guess that's where I was wrong. The above code snippet (with or without the errors) was an attempt to demonstrate that with a concrete example.

Joanne Neal
Rancher

Joined: Aug 05, 2005
Posts: 3601
    
  15
Originally posted by Peter Cimring:

If I understand the answers posted thus far, accessor methods will return a ref to the original data member - presuming that the data member is an object, not a primitive.


That depends on how you write your accessor methods. They can return a reference to the original object or you can create a new object and return a reference to that.

Originally posted by Peter Cimring:
Since the whole idea of getters and setters is to prevent accidental / unauthorized change to the object's data member, I always had a (vaguely formed) notion that the 'getter' would return a copy (clone) of the object - in keeping with the idea that you could only change the data member via the 'setter'.


It's certainly a good idea to return a reference to a different object for the reasons you've stated. In the case of a String it's not actually necessary because Strings are immutable so you couldn't change them anyway.
Peter Cimring
Greenhorn

Joined: Aug 13, 2008
Posts: 5
Thanks Joanne - that's clarified it quite nicely.
 
GeeCON Prague 2014
 
subject: Is this a viable way to 'deep clone' objects?