Re: super.clone() puzzlement



Bent C Dalager wrote:
On 2008-11-22, Arne Vajhøj <arne@xxxxxxxxxx> wrote:
Based on the wording:
"Typically, this means copying any mutable objects that comprise the internal "deep structure" of the object being cloned and replacing the references to these objects with references to the copies. If a class contains only primitive fields or references to immutable objects, then it is usually the case that no fields in the object returned by super.clone need to be modified."

and what is common practice, then I will expect a clone
to contain a copy, but only of first level - also
known as shallow clone.

If not then the docs better contain a warning in 72 point, red,
bold, blinking.

What the doc for Object.clone() basically says is that you have pretty
much no guarantee whatsoever what clone() is actually going to do. It
also says that it is ok, and even expected occasionally, that some
implementations break with what may be considered common practice (aka
"convention" in the actual doc). This being the case, it is advisable
to treat /all/ implementations of clone() as if they had a warning in
72 point red bold blinking letters saying "caveat emptor". Blindly
using a class' clone() implementation in the hope that it behaves as
you think it should is a recipe for disaster.

Java is not C.

I will expect code to follow convention or document its deviation.

As a case in point, while you say that you would expect only a shallow
copy, the Object.clone() doc you quote above specifies that in the
case of contained mutable objects it is necessary that deep copies be
made in the conventional case. But, again, this is only presented as a
guideline.

No. It talks about deep structures. It does not talk about
deep clone.

If you happen to have Joshua Bloch Effective Java 1st edition, then
item 10 covers the clone method in detail.

<quote>
The resulting mechanism is extralinguistic: It creates an
object without calling a constructor.
</quote>

And he gives two examples that show it is only the first
level fields that get cloned - not what they contain.

I assume he knows what was the intention with the
construct.

But he did probably not invent it, because he also has
some pretty harsh words about both the Cloneable interface
and about the text in the docs for the clone method.

Arne


.



Relevant Pages

  • Re: super.clone() puzzlement
    ... this means copying any mutable objects that comprise the ... internal "deep structure" of the object being cloned and replacing the ... references to these objects with references to the copies. ... known as shallow clone. ...
    (comp.lang.java.programmer)
  • Re: Deep Clones
    ... Clone() from the ICloneable interface: ... protected virutal void MakeDeep{/* replace shallow member copies ... shallow member copies with deep copies */} ... if a property deep deep down in one object references some ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Deep Clones
    ... public virtual object Clone() ... furhter clones of members where appropriate */} ... shallow member copies with deep copies */} ... For instance, if a property deep deep down in one object references some kind of database session, do you clone this as well? ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Newbie Q: Serialization and Me
    ... private / inner class and then use your serialization trick on *that* ... Dim bf As BinaryFormatter = New BinaryFormatter ... I have 2 objects and I want to make a deep copy of one into the ... I effectively want to clone an object into Me. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Copying a StyledDocument deeply
    ... references point to the very same object. ... of deep-cloning (copying) them. ... way might be to serialize/deserialize them to get a clone. ... you might want to examine the source code ...
    (comp.lang.java.gui)

Loading