Presents your JAVA E-NEWSLETTER for October 30, 2003 <-------------------------------------------> CUSTOMIZE SERIALIZATION BEHAVIOR FOR YOUR CLASSES When you're serializing Java objects, there are times when you may want to avoid serializing certain properties of your Java class. When it comes to avoiding the serialization of specific class members, you have three choices: declare the field transient; provide a list of class members that should be serialized; or handle the actual reading and writing of instance data yourself. The easiest method for preventing serialization is to simply specify the class property as transient. When a Serializable class property is marked as transient, the property will not be written to or read from an instance's serialized stream. Here's an example of declaring a field transient: private transient long timestamp; It doesn't get much easier than that. The second way to control which fields get serialized is to define the field serialPersistentFields for your class. When you define this field in your Serializable class, only the classes described with entries in this field will be serialized and deserialized when the object is written to and read from the object's byte stream. Telling Java what data should be persistent, rather than what data fields shouldn't, is the opposite of the transient declaration. The third way is to take over the task of writing and reading object state yourself. A class that is Serializable but not Externalizable only needs to provide the methods writeObject() and readObject(). When these methods are called, the class itself can decide which information should be serialized and which should not. An Externalizable class can also implement the methods readExternal() and writeExternal() to override the object's serialization behavior. Java's object serialization mechanism gives the programmer wide latitude in determining which properties of a class to serialize. The solutions range from simply adding a keyword to taking full responsibility for storing data. Explore all of your serialization options before deciding on a game plan to choose the right approach for your applications. David Petersheim is a Senior Java Developer with Genscape, Inc. He designs and develops server-side applications to acquire and process real-time energy data. ----------------------------------------