-Varun -
(My Blog) - Online Certifications - Webner Solutions
Steve
Originally posted by Rory Marquis:
However, as Steve said the clone will simply copy the reference to the String anyway so this wouldn't make any difference until you disposed of the original.
Joanne
Originally posted by Joanne Neal:
And it wont make any difference when you dispose of the original either. The reference in your clone will still point at the String object.
Originally posted by Rory Marquis:
hi Joanne
Not if the String isn't cloned by the overriding clone() method, which is what I was suggesting Then the String would be GCed when the original List was, just leaved the cloned objects sans String.
[ November 06, 2008: Message edited by: Rory Marquis ]
Steve
-Varun -
(My Blog) - Online Certifications - Webner Solutions
Originally posted by Steve Luke:
Incorrect. No Object would get GCed if there is at least one reference to the object. When you copy the reference into the new clone the Strings have 2 references to them (at least), the one in the original object and the second in the new object. When the original object gets GCed the Strings still have references to them and so can't be collected.
just override clone [method] in your object and clone the object and setting the strings to null.
where the clone does have a reference to the string, so disposing of the original will make no difference.However, as Steve said the clone will simply copy the reference to the String anyway so this wouldn't make any difference until you disposed of the original.
Joanne
-Varun -
(My Blog) - Online Certifications - Webner Solutions
Originally posted by Joanne Neal:
Yes, but Steve and I are referring to your second sentence where the clone does have a reference to the string, so disposing of the original will make no difference.
However, as Steve said the clone will simply copy the reference to the String anyway so this wouldn't make any difference until you disposed of the original.
However, as Steve said the [basic/original/non overidden] clone will simply copy the reference to the String anyway so this [setting the clone string to null] wouldn't make any difference [in comparison to the original way] until you disposed of the original [where upon the original string would be GCed and you would no longer have a reference to it thus freeing up memory].
Originally posted by Varun Chopra:
Output:
s = null
cloned = Hello
Steve
Originally posted by Rory Marquis:
Hope that clears everything up.
However, as Steve said the clone will simply copy the reference to the String anyway so this wouldn't make any difference until you disposed of the original.
Not if the String isn't cloned by the overriding clone() method, which is what I was suggesting Then the String would be GCed when the original List was, just leaved the cloned objects sans String.
Steve
Originally posted by Steve Luke:
It sounds like you were saying that we would have to clone the String in order to keep them alive in the cloned object.
Yes, I can see that I wasn't as clear as I should have been there.
Since none of the previous posts actually suggested cloning the String, mine only suggested letting the String references get copied, AND you never refer back to re-nulling the copies of the String, this again makes it easy to mis-interpret (or rather hard to interpret the way you meant it to be).
You are correct, I should have referred back to myself. I didn't mean to cause confusion on something that should be simple
Why do I bother writing this? I don't want to dwell, and would normally have stopped at 'It does now', but it irks me when you repeatedly said 'read the thread' as if we hadn't and what you were saying was obvious if only we would read it.
We did read the thread. What you said sounded wrong. Only now after you fill in the context of what was going on in your head (but not written in your posts) does it clear things up.
Sorry, this wasn't meant as a rant, I just wanted you to understand that you weren't really being fair when you repeatedly said 'if you would read the thread...'.
code:
Output:
s = null
cloned = Hello
Hi Varun, what you showed is the right concept we are talking about. You shouldn't consider
String cloned = s;
a form of cloning. It isn't, it is just a reference copy and can only be substituted for cloning when the object in question is immutable.
-Varun -
(My Blog) - Online Certifications - Webner Solutions