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

Thursday, January 3, 2013

Serialization in Java


Serialization is used to save the object and all of its instance variables (except transient) in an external file mostly with .ser extension. With the help of Serialization you can reconstruct the same object latter.

The Serialization is done with the help of below two methods

// serialize objects and write them to a stream
ObjectOutputStream.writeObject() 

// read the stream and deserialize objects
ObjectInputStream.readObject()

About the example - We have a Dog class, which we will be Serializing. Note that Class which to be serialize must implements the Serializable interface. Serializable is a marker interface, it has no methods to implement. In SerializeDog.java we will create a Dog object d1 serialize it and write it to a dog.ser file and latter we will deserialize the object.

Please see the java codes below