Java Reference
In-Depth Information
The first of three steps is converting the object to a serial stream of bytes. First, type
information gets written as header information to the stream, and then state information is
written. The state information consists of the values of the class members, except for the
static and transient members (assuming the default serialization mechanism, which does not
implement readObject and writeObject ; this is discussed a bit later in more detail). On the
deserialization end, the receiving remote object first reads the type information in from the
header and creates an instance of the class. If the class cannot be located (either locally or
remotely), then a ClassNotFoundException is raised. Once the class is instantiated, the field
values are read in from the stream and the values set in the marshaled object.
â– 
Note Serialized objects are a copy of the original, not a reference to them. Parameters and return values
in RMI are passed by values or copies. An object can be referenced on another machine only if the object
is a remote object—that is, the object is exported via the UnicastRemoteObject.export() method or
UnicastRemoteObject is extended.
Figure 6-2. Creating the byte stream from an object graph
Using the serialver Tool
Serialization involves implementing the java.io.Serializable interface. This interface is of
interest because it does not have any methods that require implementing. In fact, the main
purpose of implementing Serializable is to inform the JVM that the object can be serialized.
Serializable is often referred to as a marker interface. Formally a marker interface is an
interface, which does not define any methods that a class or subclass would be required to
implement but rather identifies the object as being of a certain type. To determine if a class is
serializable, use the tool serialver . To start the serialver tool, from the project root enter the
following at a command prompt:
serialver -classpath classes/. -show
Let's run the serialver tool on two of our classes, one that is serializable and one that is
not serializable. We will use the DVD class as our serializable class and DvdFileAccess as the
class that is not serializable. Figures 6-3 and 6-4 demonstrate the use of serialver on our DVD
and DvdFileAccess classes, respectively.
Search WWH ::




Custom Search