CONTENTS | PREV | NEXT Java Object Serialization Specification


1.6 Defining Serializable Fields for a Class

By default, the serializable fields of a class are defined to be the non-transient and non-static fields. These declarations can be overridden by declaring a special field in the class. For example, the declaration below duplicates the default behavior.

class List implements Serializable {
    List next;

    public final static ObjectStreamField[] serialPersistentFields
                 = {new ObjectStreamField("next", List.class)};
}
The serialPersistentFields field must be initialized with an array of ObjectStreamField objects that list the names and types of the serializable fields. The field must be static and final so it does not change during operation.

When an ObjectStreamClass is created for a class it examines the class to determine the serializable fields. It looks for the definition of the serialPersistentFields and if it is not found then the list of serializable fields is created from the non-transient and non-static fields of the class.

By using serialPersistentFields to define the Serializable fields for a class, there no longer is a limitation that a serialiable field must be a field within the current definition of the Serializable class. The writeObject and readObject methods of the Serializable class can map the current implementation of the class to the serializable fields of the class. Thus, the data representation for a Serializable class can change in a later releases, as long as it maintains the mapping back to its Serializable fields that must remain compatibile across release boundaries.



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