Java Reference
In-Depth Information
JAVA UPDATE J ava 2
J ava 2
v 5 . 0
Implementing the Serializable Interface
v 5 . 0
In an earlier chapter, data was saved to a disk file using the Java
Input/Output classes from the java.io package; however, this action did not
include saving objects. The Input/Output classes are extended with support for
encoding an object into a byte stream that subsequently can be stored to disk.
They also support the complementary reconstruction of the object from the
stream. The process of encoding the state of an object into a stream is called
serialization . Serialization encodes not only the data in a class, but also other
information, which includes the class name and signature. Serialization is used
in the process of storing an object to a persistent state and often carries the con-
notation of persisting to permanent storage, although it need not do so. Restor-
ing a serialized object for use in a program is called deserialization . Recall that
an object also consists of methods as well as its attributes. While the methods are
not saved with each object, the object is marked so programs using a modified
definition of the object cannot access a previous version of the object.
Serializing Enums
The new class Enum
implements java.io.Seri-
alizable—therefore,
Enum objects may be
serialized. However,
Enum constants are seri-
alized in a different
manner than typical
serializable or externaliz-
able objects. While the
differences may be
ignored in many cases,
they may affect cus-
tomization of the seriali-
zation process.
When an object uses
the default serialization
mechanism, the class of
the object, the class sig-
nature, and the values
of all non-transient and
non-static fields are
written to the persistent
storage. Also, each seri-
alizable class is uniquely
identified by a serialVer-
sionUID field, either
declared in the class or
computed from the
class definition by the
Java virtual machine. Of
course, serialization only
writes out fields of
objects that implement
the java.io.Serializable
interface.
For Enum objects, only
the name is serialized,
not the field values of
the constant. Also, the
serialization process for
Enum constants cannot
be customized; any
class-specific writeObject
and writeReplace meth-
ods defined by Enum
types are ignored during
serialization. Finally, for
Enums, any serialVer-
sionUID field declaration
is ignored as all Enum
types have a fixed seri-
alVersionUID of 0L.
Serialization and Deserialization
Serialization is simply the conversion of an object into a stream.
Serializing to a byte stream allows the object to be manipulated as
binary data, which can be saved to persistent storage or transmit-
ted across a network. Here is an analogy: think of a brick house as
the object. Serialization would be taking the house apart, brick by
brick. Transmitting the serialized object across a network would be
like sending the bricks in sequence (serially) to a new location.
Deserialization would be rebuilding the house, one brick at a time.
For an object to be serialized, it must implement the Serializable interface,
which has no methods or fields. An interface with no methods or fields is called
a marker interface , because it serves to identify the object only semantically; in
this case, it is identified as serializable and deserializable. Classes that inherit
from a serializable parent also are serializable.
In this application, Password objects are serialized so they can be stored in a
database. Consequently, the Password class from the previous chapter must be
modified to implement the Serializable interface. Figure 11-4 on the next page
displays the modified code for the Password class.
Search WWH ::




Custom Search