Purpose of the serialVersionUID in Java

The purpose of the serialization version UID is to keep track of different versions of a class in order to perform valid serialization of objects.
The idea is to generate an ID that is unique to a certain version of an class, which is then changed when there are new details added to the class, such as a new field, which would affect the structure of the serialized object.
Always using the same ID, such as 1L means that in the future, if the class definition is changed which causes changes to the structure of the serialized object, there will be a good chance that problems when trying to deserialize an object.
If the ID is omitted, Java will actually calculate the ID for you based on fields of the object, but I believe it is an expensive process, so providing one manually will improve performance.
Sample Code :
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import com.example.bean.Employee;
public class Serialization {
public static void main(String[] args){
try {
FileOutputStream out = new FileOutputStream(“save.ser”);
ObjectOutputStream oos = new ObjectOutputStream(out);
Employee e = new Employee(100, “Raaj”, 67890.0);
System.out.println(e);
oos.writeObject(e);
oos.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import com.example.bean.Employee;
public class Deserialization {
/**
* @param args
* @throws IOException
* @throws ClassNotFoundException
*/
public static void main(String[] args) throws IOException, ClassNotFoundException {
try {
FileInputStream in = new FileInputStream(“save.ser”);
ObjectInputStream ois = new ObjectInputStream(in);
Employee e = (Employee) ois.readObject();
System.out.println(e);
ois.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}

import java.io.Serializable;
public class Employee implements Serializable{
/**
serialVersionUID - serialVersionUID. 
*/
private static final long serialVersionUID = 834632471724720579L;
private int no;
private String name;
private double salary;
private String department;
public Employee(int no,String name,double salary) {
this.name = name;
this.no = no;
this.salary = salary;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return “”+no+” “+name+” “+salary;
}
......
// Setter and Getter methods
.....
}
Note: 
  • Commented private static final long serialVersionUID = 834632471724720579L;
  • Serializ Employee object,Changed Employee object
  • Tried to Deserialize Employee ,got the following exception
java.io.InvalidClassException: com.example.bean.Employee; local class incompatible: stream classdesc serialVersionUID = 8344632471724720579, local class serialVersionUID = -3954989914910331080
at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:519)
at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1546)
at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1460)
at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1693)
at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1299)
at java.io.ObjectInputStream.readObject(ObjectInputStream.java:339)
at com.example.Deserialization.main(Deserialization.java:20)
To avoid this problem and deserialize anyway we use “serialVersionUID” to maintain same one in all cases.


Enter your email address to get our daily JOBS & INTERVIEW FAQ's Straight to your Inbox.

Make sure to activate your subscription by clicking on the activation link sent to your email