CONTENTS | PREV | NEXT | Java Object Serialization Specification |
Serialization provides two mechanisms for accessing the serializable fields in a stream. The default mechanism requires no customization while the alternative allows the class to explicitly access the fields by name and type.The default mechanism is used automatically when reading or writing objects that implement the Serializable interface and do no further customization. The serializable persistent fields are mapped to the corresponding fields of the class and values are written to the stream from those fields or read in and assigned respectively. If the class provides
writeObject
andreadObject
methods the default mechanism can be invoked by callingdefaultWriteObject
anddefaultReadObject
. When implementing these methods the class has an opportunity to modify the data values before they are written or after they are read.When the default mechanism cannot be used, the serializable class can use the
putFields
ofObjectOutputStream
to put the values for the serializable fields into the stream. ThewriteFields
method ofObjectOutputStream
puts the values in the in the correct order and 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.