Searching...
Thursday, 25 April 2013

What is the purpose of serialVersionUID

08:20

What is serialVersionUID ?

The serialVersionUID is an identifier for serialized objects. If the serialVersionUID of a serialized object doesn't match to the current serialVersionUID of that class, then the deserialization throws an InvalidClassException.

Here is one example, we have a customer class which is having the default serilVersionUID

package com.vinod.test;

import java.io.Serializable;

public class Customer implements Serializable{

    /**
     *
     */

    private static final long serialVersionUID = 1L;
    private String name;
    private String address;
   
    public Customer(String name, String address) {
        super();
        this.name = name;
        this.address = address;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
    public String getAddress() {
        return address;
    }
    public void setAddress(String address) {
        this.address = address;
    }
    @Override
    public String toString() {
        return "Customer [name=" + name + ", address=" + address + "]";
    }
   
}
 

Let us serialize this object

package com.vinod.test;

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class SerializeTest {
    public static void main(String[] args) {
        // Serialization
        Customer customer = new Customer("vinod", "bangalore");
        System.out.println("Serialization started.");
        FileOutputStream fos;
        try {
            fos = new FileOutputStream("customer.ser");
            ObjectOutputStream oos;
            oos = new ObjectOutputStream(fos);
            oos.writeObject(customer);
            oos.close();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();

        }
        System.out.println("Serialization completed.");

    }

}
 

Now change the serialVersionUID in the customer class and try to Deseralize

    private static final long serialVersionUID = 2L;

package com.vinod.test;

import java.io.FileInputStream;
import java.io.ObjectInputStream;

public class DeserializeTest {

    public static void main(String[] args) {
        try {
            FileInputStream fis = new FileInputStream("customer.ser");
            ObjectInputStream ois = new ObjectInputStream(fis);
            Customer deserializedObj;
            deserializedObj = (Customer) ois.readObject();
            System.out.println("DeSerialized SerializableObject: " + deserializedObj);
        } catch (Exception e) {
            System.out.println(e);
        }
    }

}
 

Output

java.io.InvalidClassException: com.vinod.test.Customer; local class incompatible: stream classdesc serialVersionUID = 1, local class serialVersionUID = 2

To avoid this casting exception we have to keep the same serialVersionUID  for serializable objects and de-serialization will be successfull. If we are not adding this version uid  JVM will generate add the default id for each objects and it is possible to do the serialization and de-serialization but after the serialization is there any changes happened in the serializable class then it will throw the Invalid cast exception during de-serialization (Objects wont match)
eg: If i am adding below phone number details to Customer class after the serialization and it will throw the invalid cast exception while de-serialization (if we have same serialVersionUID over there in the class it wont throw exception) 

Done!!--

 

0 comments: