❮ Previous Next ❯

Java Serialization

The process of writing state of an object to a file is called Serialization but strictly speaking it is the process of converting an object from java supported form into either file supported form or neither supported form.

By using FileOutputStream and ObjectOutputStream classes we can implement Serialization.


The process of reading state of an object from the file is called Deserialization but strictly speaking it is the process of converting an object from either Filesupportedform or network supported form into java supported form.

By using FileInputStream and ObjectInputStream classes we can implement Deserialization.


import java.io.*;
class Test implements Serializable
int x=15;
int y=25;
class Serialization
public static void main(String args[]) throws Exception
Test t1=new Test();
FileOutputStream fs=new FileOutputStream("demo.ser");
ObjectOutputStream os=new ObjectOutputStream(fs);
System.out.println("successfully serialized");
FileInputStream fi=new FileInputStream("demo.ser");
ObjectInputStream oi=new ObjectInputStream(fi);
Test t2=(Test)oi.readObject();
System.out.println(t2.x+" "+t2.y);

successfully serialized
15  25

We can serialize only serializable objects ,an object is said to be serializable if and only if the corresponding class implements Serializable(I) interface.

Serializable(I) interface present in java.io package and it doen't contain any methods it is a Marker Interface.

If we are trying to serialize a non serializable object then we will get RuntimeException saying NotSerializableException.

transient keyword

transient modifier(keyword)applicable only for variables but not for methods and classes.

At the time of serialization if we donít want to save the value of a particular variable to meet security constraints then we should declare that variable as transient.

While performing serialization JVM ignores original value of transient variable and save default value to the file.

Hence transient means not to serialize.

transient vs static:-

static variable is not part of object state and hence it wonít participate in serialization due to this declaring static variable as transient there is no use.

final variables will be participated in serialization directly by the value hence declaring a final variable as transient there is no impact.

final vs transient:-


We can serialize any number of objects to the file but in which order we serialiable in the same order only we have to deserialize i.e order of objects is serialization.


Dog d1=new Dog();
Cat c1=new Cat ();
Rat r1=new Rat();
FileOutputStream fos=new FileOutputStream("abc.ser");
ObjectOutputStream oos=new ObjectOutputStream(fos);
FileInputStream fis=new FileInputStream("abc.ser");
ObjectInputStream ois=new ObjectInputStream(fis);
Dog d2=(Dog) ois.readObject();
Cat c2=(Cat) ois.readObject();
Rat r2=(Rat) ois.readObject();

If we donít know order of objects in serialization:-


FileInputStream fis=new FileInputStream("abc.ser");
ObjectInputStream ois=new ObjectInputStream(fis);
Object o=ois.readObject();
If(o instance of Dog)
Dog d2-(Dog)o;
//perform Dog specific functionality
else if (o instance of Cat)
Cat c2=(Cat)o;
//perform Cat specific functionality
else if(o instance of Rat)
❮ Previous Next ❯


Popular Links

Contact Us

address Plot No-N6/530,2ND Floor
Nayapalli,Jayadev Vihar
      Email: info@silantechnology.com
      Phone: 0674-2361252
39877, sundale dr, apt#101, Fremont, California 94538, USA
       Phone: +1(262)388-7619

facebook twitter google linked in

© 2018 Silan Technology. All Rights Reserved