Java Reference
In-Depth Information
19.6.1 The
Serializable
Interface
Not every object can be written to an output stream. Objects that can be so written are said to
be
serializable
. A serializable object is an instance of the
java.io.Serializable
inter-
face, so the object's class must implement
Serializable
.
The
Serializable
interface is a marker interface. Since it has no methods, you don't
need to add additional code in your class that implements
Serializable
. Implementing this
interface enables the Java serialization mechanism to automate the process of storing objects
and arrays.
To appreciate this automation feature, consider what you otherwise need to do in order to
store an object. Suppose you want to store a
JButton
object. To do this you need to store all
the current values of the properties (e.g., color, font, text, alignment) in the object. Since
JButton
is a subclass of
AbstractButton
, the property values of
AbstractButton
have
to be stored as well as the properties of all the superclasses of
AbstractButton
. If a prop-
erty is of an object type (e.g.,
background
of the
Color
type), storing it requires storing all
the property values inside this object. As you can see, this would be a very tedious process.
Fortunately, you don't have to go through it manually. Java provides a built-in mechanism to
automate the process of writing objects. This process is referred to as
object serialization
,
which is implemented in
ObjectOutputStream
. In contrast, the process of reading objects
is referred to as
object deserialization
, which is implemented in
ObjectInputStream
.
Many classes in the Java API implement
Serializable
. The utility classes, such as
java.util.Date
, and all the Swing GUI component classes implement
Serializable
.
Attempting to store an object that does not support the
Serializable
interface would cause
a
NotSerializableException
.
When a serializable object is stored, the class of the object is encoded; this includes the
class name and the signature of the class, the values of the object's instance variables, and the
closure of any other objects referenced by the object. The values of the object's static vari-
ables are not stored.
serializable
serialization
deserialization
NotSerializableException
Note
nonserializable fields
If an object is an instance of
Serializable
but contains nonserializable instance
data fields, can it be serialized? The answer is no. To enable the object to be serialized,
mark these data fields with the
transient
keyword to tell the JVM to ignore them
when writing the object to an object stream. Consider the following class:
transient
public class
C
implements
java.io.Serializable {
private int
v1;
private
static
double
v2;
private
transient
A v3 =
new
A();
}
class
A { }
// A is not serializable
When an object of the
C
class is serialized, only variable
v1
is serialized. Variable
v2
is
not serialized because it is a static variable, and variable
v3
is not serialized because it is
marked
transient
.
If
v3
were
not
marked
transient
,
a
java.io.NotSerializableException
would occur.
Note
duplicate objects
If an object is written to an object stream more than once, will it be stored in multiple
copies? No, it will not. When an object is written for the first time, a serial number is
created for it. The JVM writes the complete contents of the object along with the serial