Serialization and Deserialization in Java With Example
Serialization and Deserialization in Java With Example
org
8-9 minutes
Serialization is a mechanism of converting the state of an object into a byte stream. Deserialization is the reverse
process where the byte stream is used to recreate the actual Java object in memory. This mechanism is used to
persist the object.
The byte stream created is platform independent. So, the object serialized on one platform can be deserialized
on a different platform.
1 of 10 2/6/2018, 11:42 PM
The ObjectOutputStream class contains writeObject() method for serializing an Object.
Advantages of Serialization
1. To save/persist state of an object.
2. To travel an object across a network.
Only the objects of those classes can be serialized which are implementing java.io.Serializable interface.
Serializable is a marker interface (has no data member and method). It is used to “mark” java classes so that
objects of these classes may get certain capability. Other examples of marker interfaces are:- Cloneable and
Remote.
2 of 10 2/6/2018, 11:42 PM
Points to remember
1. If a parent class has implemented Serializable interface then child class doesn’t need to implement it but vice-
versa is not true.
2. Only non-static data members are saved via Serialization process.
3. Static data members and transient data members are not saved via Serialization process.So, if you don’t want
to save value of a non-static data member then make it transient.
4. Constructor of object is never called when an object is deserialized.
5. Associated objects must be implementing Serializable interface.
Example :
SerialVersionUID
The Serialization runtime associates a version number with each Serializable class called a SerialVersionUID,
which is used during Deserialization to verify that sender and reciever of a serialized object have loaded classes
for that object which are compatible with respect to serialization. If the reciever has loaded a class for the object
that has different UID than that of corresponding sender’s class, the Deserialization will result in an
InvalidClassException. A Serializable class can declare its own UID explicitly by declaring a field name.
It must be static, final and of type long.
i.e- ANY-ACCESS-MODIFIER static final long serialVersionUID=42L;
If a serializable class doesn’t explicitly declare a serialVersionUID, then the serialization runtime will calculate a
default one for that class based on various aspects of class, as described in Java Object Serialization
Specification. However it is strongly recommended that all serializable classes explicitly declare serialVersionUID
value, since its computation is highly sensitive to class details that may vary depending on compiler
implementations, any change in class or using different id may affect the serialized data.
It is also recommended to use private modifier for UID since it is not useful as inherited member.
serialver
3 of 10 2/6/2018, 11:42 PM
The serialver is a tool that comes with JDK. It is used to get serialVersionUID number for Java classes.
You can run the following command to get serialVersionUID
Example 1:
import java.io.*;
public int a;
public String b;
this.a = a;
this.b = b;
class Test
4 of 10 2/6/2018, 11:42 PM
Demo object = new Demo(1, "geeksforgeeks");
try
out.writeObject(object);
out.close();
file.close();
catch(IOException ex)
System.out.println("IOException is caught");
try
object1 = (Demo)in.readObject();
5 of 10 2/6/2018, 11:42 PM
in.close();
file.close();
catch(IOException ex)
System.out.println("IOException is caught");
catch(ClassNotFoundException ex)
System.out.println("ClassNotFoundException is caught");
Output :
Example 2:
6 of 10 2/6/2018, 11:42 PM
import java.io.*;
class Emp implements Serializable {
129348938L;
transient int a;
static int b;
String name;
int age;
this.name = name;
this.age = age;
this.a = a;
this.b = b;
7 of 10 2/6/2018, 11:42 PM
System.out.println("a = " + object1.a);
try {
(filename);
(file);
out.writeObject(object);
out.close();
file.close();
printdata(object);
object.b = 2000;
8 of 10 2/6/2018, 11:42 PM
System.out.println("IOException is caught");
object = null;
try {
(filename);
(file);
object = (Emp)in.readObject();
in.close();
file.close();
printdata(object);
System.out.println("IOException is caught");
System.out.println("ClassNotFoundException" +
" is caught");
9 of 10 2/6/2018, 11:42 PM
}
Output:
This article is contributed by Mehak Narang and Shubham Juneja. If you like GeeksforGeeks and would like to
contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to
contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other
Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic
discussed above.
10 of 10 2/6/2018, 11:42 PM