Java Design Pattern
Introduction to Java 10
Introduction to Java 11
Introduction to Java 12

Java Serialization & De-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.

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 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)

About the Author

Silan Software is one of the India's leading provider of offline & online training for Java, Python, AI (Machine Learning, Deep Learning), Data Science, Software Development & many more emerging Technologies.

We provide Academic Training || Industrial Training || Corporate Training || Internship || Java || Python || AI using Python || Data Science etc