CONTENTS | PREV | NEXT Java Object Serialization Specification


1.7 Accessing Serializable Fields of a Class

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 and readObject methods the default mechanism can be invoked by calling defaultWriteObject and defaultReadObject. 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 of ObjectOutputStream to put the values for the serializable fields into the stream. The writeFields method of ObjectOutputStream puts the values in the in the correct order and writes them to the stream using the existing protocol for serialization. Correspondingly, the readFields method of ObjectInputStream reads the values from the stream and makes them available to the class by name in any order.



CONTENTS | PREV | NEXT
Copyright © 1997 Sun Microsystems, Inc. All Rights Reserved.