What is serialVersionUID? In serialization process at runtime each serializable class is associated with a version number, called a serialVersionUID, and this versionUID is used to ensure that during deserialization the same class is loaded which was used during serialization process. If serialVersionUID is not declared explicitly then JVM will do it for you automatically.
Problem occur if not defining serialVersionUID: serialVersionUID is used to control the version of class.If serialVersionUID is not declared in the class and if there is any incompatible modification made in class, then we would not be able to deserialize this class as serialVersionUID generated by java compiler for modified class will be different from old serialized class and deserialization process will throw java.io.InvalidClassException.
In below example, we commented serialVersionUID and serialized class StudentInformation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.Serializable; class StudentInformation implements Serializable { // private static final long serialVersionUID = 1L; private Integer rollNo; private String name; public StudentInformation() { System.out.println("No argument constructor is called"); } public StudentInformation(Integer rollNo,String name) { System.out.println("Parameterised constructor is called"); this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "Student Information is rollNo : " + rollNo + "and Student name is : "+name ; } } public class SerializableDemo { public static void main(String[] args) { StudentInformation object1 = new StudentInformation(21,"Shobhna"); try { OutputStream fout = new FileOutputStream("ser.txt"); ObjectOutput oout = new ObjectOutputStream(fout); System.out.println("Serializing StudentInformation object"); oout.writeObject(object1); fout.close(); oout.close(); System.out.println("Serialization process completed."); InputStream inputStream=new FileInputStream("ser.txt"); } catch (IOException e) { e.printStackTrace(); } } } |
OutPut:
Parameterised constructor is called
Serializing StudentInformation object
Serialization process completed.
Now, modify this class by adding field address and do not serialize class again as given below
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 |
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.Serializable; class StudentInformation implements Serializable { private Integer rollNo; private String name; private String address; public StudentInformation() { System.out.println("No argument constructor is called"); } public StudentInformation(Integer rollNo,String name) { System.out.println("Parameterised constructor is called"); this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "Student Information is rollNo : " + rollNo + "and Student name is : "+name ; } } public class SerializableDemo { public static void main(String[] args) { StudentInformation object1 = new StudentInformation(21,"Shobhna"); } } |
OutPut:
Parameterised constructor is called
Now , try to deserialize the saved class StudentInformation , you will get InvalidClassException
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 |
import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.ObjectInput; import java.io.ObjectInputStream; import java.io.ObjectOutput; import java.io.ObjectOutputStream; import java.io.OutputStream; import java.io.Serializable; class StudentInformation implements Serializable { // private static final long serialVersionUID = 1L; private Integer rollNo; private String name; private String address; public StudentInformation() { System.out.println("No argument constructor is called"); } public StudentInformation(Integer rollNo,String name) { System.out.println("Parameterised constructor is called"); this.rollNo = rollNo; this.name = name; } @Override public String toString() { return "Student Information is rollNo : " + rollNo + "and Student name is : "+name ; } } public class SerializableDemo { public static void main(String[] args) { try { InputStream inputStream=new FileInputStream("ser.txt"); ObjectInput outInput=new ObjectInputStream(inputStream); System.out.println("\nDeSerializing StudentInformation objects"); StudentInformation std=(StudentInformation)outInput.readObject(); System.out.println(std); inputStream.close(); outInput.close(); System.out.println("Object DeSerialization completed."); } catch (Exception e) { e.printStackTrace(); } } } |
1 2 3 4 5 6 7 8 9 10 11 |
<span style="text-decoration: underline;"><strong>OutPut:</strong></span> java.io.InvalidClassException: StudentInformation; local class incompatible: stream classdesc serialVersionUID = 8086333747608896269, local class serialVersionUID = -2722271583385024093 at java.io.ObjectStreamClass.initNonProxy(ObjectStreamClass.java:616) at java.io.ObjectInputStream.readNonProxyDesc(ObjectInputStream.java:1623) at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:1518) at java.io.ObjectInputStream.readOrdinaryObject(ObjectInputStream.java:1774) at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1351) at java.io.ObjectInputStream.readObject(ObjectInputStream.java:371) at SerializableDemo.main(SerializableDemo.java:57) |