Chapter 10. Object Serialization
To serialize an object means to write
its complete internal state (including its private
state) to a byte stream in such a way
that a copy of the object can be reconstituted at some later time. When
an object is serialized, the entire object graph of all the objects it
refers to are serialized along with it. This means it’s possible to
serialize complex data structures such as binary trees. It’s also
possible to serialize applets and complete GUI component hierarchies.
Serialization is a critical piece of Remote Method Invocation (see Chapter 21) because it allows objects (as
long as they are Serializable
) to be
passed across the network. Serialization also provides a convenient way
to produce a “deep clone” of an object graph. Serialization can also be
used as a trivial technique for saving application state: just serialize
the data structures to disk. This requires care, however, because the
serialization file format is sensitive to implementation changes in your
classes. In Java 1.4, the java.beans
package (see Chapter 15) provides a
JavaBeans-based persistence mechanism that is often a better alternative
for long-term persistence.
This chapter shows how to serialize and deserialize objects, and how to write serializable classes that make simple use of the serialization mechanism. Serialization is actually a surprisingly complex problem, and the serialization mechanism has been updated with each release of Java. This chapter does not attempt ...
Get Java Examples in a Nutshell, 3rd Edition now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.