Re: Backing Up Objects



On Jul 10, 12:21 pm, Hal Vaughan <h...@xxxxxxxxxxxxxxxxxxxx> wrote:
But if I'm writing a copy method, I have to write something that will take
each Object I have in my class and duplicate it, which leads back to the
original problem: How do I make an actual copy of an object? Are there
types of Objects where clone() does an actual clone and create a new
Object?

If you're just copying, say, an ArrayList of Foos where Foos are some
simple value objects that implement Cloneable, you can use a custom
copying method to do it easily.

If you've got a substantially more complex data structure which would
have a nontrivial traversal algorithm and contains an open-ended
variety of things, you're probably going to have a much easier time
using serialization. That takes the matter of writing the traversal
algorithm and getting it right out of your hands and leaves you
without much more to do for most of your classes than Cloneable. With
Cloneable you need to tack on "implements Cloneable" and include

public Object clone () {
super.clone();
}

to make "clone" public, at minimum. And you've still introduced a
"backdoor constructor" of sorts.

Serializability in a lot of cases just means tacking on "implements
java.io.Serializable" and including
private static final long serialVersionUID = 1;

and increasing this by one every time you make certain changes to the
class in question (basically any change where there was a non-
transient field with a particular name and there no longer is or a
field changes type other than to a supertype of the old type, and any
change where a non-transient field starts existing and cannot be null,
false, or zero by default). More complex stuff is only infrequently
needed, e.g. a singleton should have every field transient and a
readResolve method that returns the singleton instance from a static
field if for some reason it's serializable. (A singleton value object
such as a Null or a NaN or an Infinity, for example, which is a
subclass of some more general value type class.)

.



Relevant Pages

  • Re: Backing Up Objects
    ... types of Objects where clone() does an actual clone and create a new ... So if it's at all complex, I'm better using serialization? ... serialization means writing the data to a stream. ... readResolve method that returns the singleton instance from a static ...
    (comp.lang.java.programmer)
  • 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: Why cant I copy a collection class from one instance to another?
    ... it is common to true object programming environments. ... still write a clone method, but the properties and methods of the object ... The side benefit of this serialization process is that the code to clone ...
    (comp.databases.ms-access)
  • Re: Saving decision tree
    ... Some objects provide a Clone function that returns a duplicate ... Public Property Let Copy ... ' code to de-serialize class data ... and Serialization are related as they are ...
    (microsoft.public.vb.general.discussion)
  • RE: Making a cloned or copied object...
    ... You can use binary serialization to create a clone or your object that is ... public class MyItem: ICloneable ...
    (microsoft.public.dotnet.general)