CONTENTS | PREV | NEXT | Java Object Serialization Specification |
The serializable fields of a class can be defined two different ways. Default serializable fields of a class are defined to be the non-transient and non-static fields. This default computation can be overridden by declaring a special field in theSerializable
class,serialPersistentFields
. This field must be initialized with an array ofObjectStreamField
objects that list the names and types of the serializable fields. The modifiers for the field are required to be private, static, and final.For example, the following declaration duplicates the default behavior.
class List implements Serializable { List next; private static final ObjectStreamField[] serialPersistentFields = {new ObjectStreamField("next", List.class)};}By usingserialPersistentFields
to define the Serializable fields for a class, there no longer is a limitation that a serializable field must be a field within the current definition of theSerializable
class. ThewriteObject
andreadObject
methods of theSerializable
class can map the current implementation of the class to the serializable fields of the class using the interface that is described in Section 1.7, "Accessing Serializable Fields of a Class." Therefore, the fields for aSerializable
class can change in a later release, as long as it maintains the mapping back to its Serializable fields that must remain compatible across release boundaries.