Serialization in java



“Serialization means storing a state of a java object by converting it to byte stream.”

Typical use of serialization:

a.       To persist data for future use

b.      To send data to a remote computer

c.       To store user session in web application

d.      To activate or passivate enterprise java beans

“Object state remains persistent in different JVM environment”- by N. K. JHA

“The value of any object’s instance variable (fields) define the state of an object. Usually, method calls changed an object’s state.”

Serializability of a class is enabled by the class implementing the java.io.Serializable interface. Classes that do not implement this interface will not have any of their state serialized or deserialized. The serialization interface has no methods or fields.

The writeObject method of ObjectOutputStream class is responsible for writing the state of the object for its particular class so that the corresponding  readObject method of ObjectInputStream class can restore it.

Some restriction:

a.       ‘transient’ keyword is used mark data member whose value is not to be save while serializing the object.

b.      static fields are not serialized because they are not part of any one particular object

c.       If object of a class conation reference of other class objects then object can only be serialize if all referenced object are serializable.

That mean :

Class A

{

int x;

}

Class B  implements Serializable

{

int a;

A a;

}

But class B not serialize because it is containg reference of non serialize class A.

No comments:

Post a Comment