Re: Backing Up Objects
- From: Twisted <twisted0n3@xxxxxxxxx>
- Date: Tue, 10 Jul 2007 19:33:56 -0000
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.)
.
- Follow-Ups:
- Re: Backing Up Objects
- From: Hal Vaughan
- Re: Backing Up Objects
- References:
- Backing Up Objects
- From: Hal Vaughan
- Re: Backing Up Objects
- From: Twisted
- Re: Backing Up Objects
- From: Hal Vaughan
- Re: Backing Up Objects
- From: Lew
- Re: Backing Up Objects
- From: Hal Vaughan
- Backing Up Objects
- Prev by Date: file locking
- Next by Date: Re: Why no "cause" constructors for NumberFormatException
- Previous by thread: Re: Backing Up Objects
- Next by thread: Re: Backing Up Objects
- Index(es):
Relevant Pages
|