Showing posts with label Serialization. Show all posts
Showing posts with label Serialization. Show all posts

Tuesday, September 29, 2009

Serialization

Serialization is a technique to write object's state to a data file. In order to be able to write the object to a file the class whose instance (object) we need to write has to implement the marker interface Serializable. This interface does not contain any methods.There are two methods that we use to serialize and deserialize an object, they are writeObject(Object) , readObject(). The writeObject method is in the ObjectOutputStream class while the readObject() method is in the ObjectInputStream class. All the instance variables of the object are saved and retrieved successfully as they were saved provided no instance variable is marked with the keyword transient. This keyword if used specifies that the state of that instance variable is not to be saved. 

Following code demonstrates how to do serialization