• Skip to primary navigation
  • Skip to content
  • Skip to primary sidebar
  • Skip to footer
  • Core Java
  • Design Patterns
  • JSP
  • Servlets
  • Building Tools
  • jQuery
  • Spring
  • Hibernate
  • Mongo DB
  • More
    • HTML
    • SCJP
    • AJAX
    • UML
    • Struts
    • J2EE
    • Testing
    • Angular JS

J2EE Reference

  • Home
  • About Us
    • Java Learning Centers
  • Contact Us

impact of serialVersionUID

What is serialVersionUID

June 13, 2017 By j2eereference Leave a Comment

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

Java
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

Java
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

Java
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();
        }
   }
}

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

 

Related Posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Difference between Stack and Heap memory
  • What is ThreadMXBean in java?
  • What is exchanger in concurrency?
  • UncaughtExceptionHandler in java

Filed Under: Core Java Tagged With: impact of serialVersionUID, serialVersionUID

Primary Sidebar

FOLLOW US ONLINE

  • View J2eereference-166104970118637’s profile on Facebook
  • View j2eereference’s profile on Twitter
  • View j2eereference’s profile on LinkedIn

Subscribe by email

Recent posts

  • Java Buzzwords
  • Anonymous Inner Class in Java
  • Network Programming – java.net Package
  • Java Regular Expressions
  • Method Local Inner Class in Java
  • URL Processing in Java
  • Iterator Design Pattern Implementation using Java
  • Strategy Design Pattern Implementation using Java
  • Decorator Design Pattern
  • Adapter Design Pattern Implementation using Java
  • JSF Composite Components
  • JSF UI Components
  • What is JavaServer Faces (JSF)?
  • GOF Design Patterns
  • History and Need for Design Patterns

Footer

Core Java
Design Patterns
JSP
Servlets
HTML
Building Tools
AJAX
SCJP
jQuery
Testing
Spring
UML
Struts
Java Centers
Java Training
Home
About Us
Contact Us
Copyright © j2eereference.com. All right reserved.