Home >> Java >> Serialization in Java

Serialization in Java

Serialization in java is just a process of converting objects into sequence of bytes due to the intention of transmitting it over channel. We consider some formal definitions here.

the process of writing state of object to file is called Serialization. It is process of converting object from java supported form to either file or network supported form. Whenever we pass object over ObjectOutputStream the serialization happens automatically. To serialize an object, the corresponding class of object must implement Serializable interface.

You may think where Serialization is useful. Very simple whenever we need to store or transmit the state of object we do serialization. For example if you want to store level of of your game application so that player can start where he has left, Serialization can be used. It is useful for developing cache supporting java program and in much more cases.

Object is deserialized while it is retrieved in application. it is reverse process of serialization. It is the process of converting an object from file or network supported form to java supported form. Or we can say it is the process of reading state of object from file or network.

Serialization in java

We will go through small example but before let’s talk about transient keyword.

In natural English language transient means temporary or that is short live. So the variables declared as transient are not serialized directly. Default value of transient variable is passed instead of original value after serialization. For example if we have a instance variable String id=”g132d”;  While serializing id will passed with default value of string null instead of “g132d”. And remember one thing transient  can only applied to the variables.

Now Look at an example.

Explanation: Here the object t1 is serialized. And later deserialized. Here the id is declared transient so we will lose its value after deserialization. So as expected output is:



  • Import java.io package
  • Handle unchecked exceptions
  • Implement Serializable interface

Fore further query comment down….

About Log Raj Bhatt

Check Also

Externalization in java

Externalization in Java Explained

In the previous post we have discussed about serialization and have written a demo program. …

Leave a Reply

Your email address will not be published. Required fields are marked *