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.
Note - There is, however, a limitation to the use of this mechanism to specify serializable fields for inner classes. Inner classes can only contain final static fields that are initialized to constants or expressions built up from constants. Consequently, it is not possible to setserialPersistentFields
for an inner class (though it is possible to set it for static member classes). For other restrictions pertaining to serialization of inner class instances, see section Section 1.10, "The Serializable Interface".