CONTENTS | PREV | NEXT | Java Object Serialization Specification |
Serialization provides two mechanisms for accessing the serializable fields in a stream:
The default mechanism is used automatically when reading or writing objects that implement theSerializable
interface and do no further customization. The serializable fields are mapped to the corresponding fields of the class and values are either written to the stream from those fields or are read in and assigned respectively. If the class provideswriteObject
andreadObject
methods, the default mechanism can be invoked by callingdefaultWriteObject
anddefaultReadObject
. When thewriteObject
andreadObject
methods are implemented, the class has an opportunity to modify the serializable field values before they are written or after they are read.When the default mechanism cannot be used, the serializable class can use the
putFields
method ofObjectOutputStream
to put the values for the serializable fields into the stream. ThewriteFields
method ofObjectOutputStream
puts the values in the correct order, then writes them to the stream using the existing protocol for serialization. Correspondingly, thereadFields
method ofObjectInputStream
reads the values from the stream and makes them available to the class by name in any order. See Section 2.2, "The ObjectOutputStream.PutField Class" and Section 3.2, "The ObjectInputStream.GetField Class." for a detailed description of the Serializable Fields API.