The transient Keyword Serialization: Writing an Object Stream

J.E.D.I.

12.12 Serialization

The Java Virtual Machine JVM supports the ability to read or write an object to a stream. This capability is called serialization, the process of flattening an object so that it can be saved to some permanent storage or passed to another object via the OutputStream class. When writing an object, it is important that its state be written in a serialized form such that the object can be reconstructed as it is being read. Saving an object to some type of permanent storage is known as persistence. The streams used for deserializing and serializing are the ObjectInputStream and the ObjectOutputStream classes, respectively. To allow an object to be serializable i.e., can be saved and retrieved, its class should implement the Serializable interface. The class should also provide a default constructor or a constructor with no arguments. One nice thing about serializability is that it is inherited, which means that we dont have to implement Serializable on every class. This means less work for programmers. You can just implement Serializable once along the class heirarchy.

12.12.1 The transient Keyword

When an object is serialized, only the objects data are preserved. Methods and constructors are not part of the serialized stream. There are some objects though that are not serializable because the data they represent constantly changes. Some examples of such objects are FileInputStream and Thread objects. A NotSerializableException is thrown if the serialization operation fails for some reason. Do not despair though. A class containing a non-serializable object can still be serialized if the reference to this non-serializable object is marked with the transient keyword. Consider the following example: class MyClass implements Serializable { transient Thread thread; try removing transient int data; some other data } The transient keyword prevents the data from being serialized. Instantiating objects from this class can now be written to an OutputStream.

12.12.2 Serialization: Writing an Object Stream

To write an object to a stream, you need to use the ObjectOutputStream class and its writeObject method. The writeObject method has the following signature: public final void writeObjectObject obj throws IOException where obj is the object to be written to the stream. The example below writes a Boolean object to an ObjectOutputStream. The Boolean class implements the Serializable interface. Thus, objects instantiated from this class can be written to and read from a stream. Introduction to Programming II Page 176 J.E.D.I. import java.io.; public class SerializeBoolean { SerializeBoolean { Boolean booleanData = new Booleantrue; try { FileOutputStream fos = new FileOutputStreamboolean.ser; ObjectOutputStream oos = new ObjectOutputStreamfos; oos.writeObjectbooleanData; oos.close; } catch IOException ie { ie.printStackTrace; } } public static void mainString args[] { SerializeBoolean sb = new SerializeBoolean; } }

12.12.3 Deserialization: Reading an Object Stream