Java Reference
In-Depth Information
17.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 wish to store an
ArrayList
object. To do this you need to store
all the elements in the list. Each element is an object that may contain other objects. 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 as
object serialization
, which is implemented in
ObjectOutputStream
.
In contrast, the process of reading objects is referred as
object deserialization
, which is imple-
mented in
ObjectInputStream
.
Many classes in the Java API implement
Serializable
. All the wrapper classes for primi-
tive type values,
java.math.BigInteger
,
java.math.BigDecimal
,
java.lang.String
,
java.lang.StringBuilder
,
java.lang.StringBuffer
,
java.util.Date
, and
java.util.ArrayList
implement
java.io.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
number into the object stream. After the first time, only the serial number is stored if the
Search WWH ::
Custom Search