How to serializable object in java

Serialization process is to converting java objects into bytes stream. This bytes stream can easily store in database or other files. This process is mostly used for persisting objects and can use in future reference.

Java provides easy to implement serialization by implement java.io.Serializable interface.

You have to just implement Serializable interface in your class. This Serializable interface automatically convert object in bytes stream, when you need to write in input output stream.

Java automatically deserializable the object while getting from stream.

Look at example

import java.io.Serializable;

public class MyClass implements Serializable{
  public void myMethod()
  {
      // java statements
  }
}

Why we need serialization

Today everyone want to use the power of object oriented programming and persisting the state and objects.

1. Suppose, if we are using clustering of web server for failover. Every web server should have information of session and objects of other web servers in cluster node. This needs to share information of object with every web server. Object can be transferred only binary form to other server. This can be done by serialization of objects in class.

2. Suppose we want to save java object into a file or in a database. This needs to convert object into a binary form stream before saving into database.

Bookmark  

 

Leave a Reply

Security Code:

 

  Random Post