Java Reference
In-Depth Information
Objects can be serialized to disk on a single machine or can be serialized across a net-
work such as the Internet, even in a case in which different operating systems are
involved. You can create an object on a Windows machine, serialize it to a Linux
machine, and load it back into the original Windows machine without error. Java trans-
parently works with the different formats for saving data on these systems when objects
are serialized.
A programming concept involved in object serialization is persistence —the capability of
an object to exist and function outside the program that created it.
16
Normally, an object that is not serialized is not persistent. When the program that uses
the object stops running, the object ceases to exist.
Serialization enables object persistence because the stored object continues to serve a
purpose even when no Java program is running. The stored object contains information
that can be restored in a program so that it can resume functioning.
When an object is saved to a stream in serial form, all objects to which it contains refer-
ences also are saved. This makes it easier to work with serialization; you can create one
object stream that takes care of numerous objects at the same time.
When several objects contain references to the same object, Java automatically ensures
that only one copy of that object is serialized. Each object is assigned an internal serial
number; successive attempts to save that object store only that number.
You can exclude some of an object's variables from serialization to save disk space or
prevent information that presents a security risk from being saved. As you will see later
today, this requires the use of the transient modifier.
Object Output Streams
An object is written to a stream via the ObjectOutputStream class.
An object output stream is created with the ObjectOutputStream( OutputStream ) con-
structor. The argument to this constructor can be either of the following:
An output stream representing the destination where the object should be stored in
serial form
n
A filter associated with the output stream leading to the destination
n
As with other streams, you can chain more than one filter between the output stream and
the object output stream.
Search WWH ::




Custom Search