are recursively located and serialized. Similarly, during the process of deserialization, all
of these objects and their references are correctly restored.
An overview of the interfaces and classes that support serialization follows.
Serializable
Only an object that implements the Serializable interface can be saved and restored by the
serialization facilities. The Serializable interface defines no members. It is simply used to
indicate that a class may be serialized. If a class is serializable, all of its subclasses are also
serializable.
Variables that are declared as transient are not saved by the serialization facilities. Also,
static variables are not saved.
Externalizable
The Java facilities for serialization and deserialization have been designed so that much of
the work to save and restore the state of an object occurs automatically. However, there are
cases in which the programmer may need to have control over these processes. For example,
it may be desirable to use compression or encryption techniques. The Externalizable interface
is designed for these situations.
The Externalizable interface defines these two methods:
void readExternal(ObjectInput inStream)
throws IOException, ClassNotFoundException
void writeExternal(ObjectOutput outStream)
throws IOException
In these methods, inStream is the byte stream from which the object is to be read, and outStream
is the byte stream to which the object is to be written.
ObjectOutput
The ObjectOutput interface extends the DataOutput interface and supports object serialization.
It defines the methods shown in Table 19-6. Note especially the writeObject( ) method. This is
called to serialize an object. All of these methods will throw an IOException on error conditions.
ObjectOutputStream
The ObjectOutputStream class extends the OutputStream class and implements the
ObjectOutput interface. It is responsible for writing objects to a stream. A constructor of
this class is
ObjectOutputStream(OutputStream outStream) throws IOException
The argument outStream is the output stream to which serialized objects will be written.
Several commonly used methods in this class are shown in Table 19-7. They will throw
an IOException on error conditions. There is also an inner class to ObjectOuputStream
called PutField. It facilitates the writing of persistent fields, and its use is beyond the scope
of this topic.
Search WWH :
Custom Search
Previous Page
Java SE 6 Topic Index
Next Page
Java SE 6 Bookmarks
Home