Thursday, September 5, 2013

Why do we need SerialVersionUID in Java ?

      

SerialVersionUID :

         The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization.

       A serializable class can declare its own serialVersionUID explicitly by declaring a field named “serialVersionUID” that must be static, final, and of type long:


ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;

        It is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization. Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value. It is also strongly advised that explicit serialVersionUID declarations use the private modifier where possible, since such declarations apply only to the immediately declaring class.serialVersionUID fields are not useful as inherited members.


take an example :

A.java:  //version1

package com.nagarjuna.java.core;

import java.io.Serializable;

public class A implements Serializable {//version1

    //private static final long serialVersionUID = 11L;
   

}//end class


try to make serialize the object of the above class using below code....


SerializationDemo.java:


package com.nagarjuna.java.core;

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

public class SerializationDemo {

    public static void main(String[] args) throws FileNotFoundException, IOException {
   
        A b1 = new A();
        ObjectOutputStream o = new ObjectOutputStream(new FileOutputStream("Demo1.srz"));
        o.writeObject(b1);
        o.close();

        System.out.println("object serialization done...");
    }

}//end class






Now modify the class A, and try to Deserialize "Demo1.srz"...

A.java:   //version2
package com.nagarjuna.java.core;

import java.io.Serializable;

public class A implements Serializable {//version1

    //private static final long serialVersionUID = 11L;

      private int x=44;
   

}//end class



package com.nagarjuna.java.core;

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

public class DeserailizationDemo {

    public static void main(String[] args) throws IOException, ClassNotFoundException {
          A b1 = new A();

        ObjectInputStream in = new ObjectInputStream(new FileInputStream("Demo1.srz"));
        b1 = (A) in.readObject();
        System.out.println("object deserialized...");

    }
}//end class


Now we will get below error...

Exception in thread "main" java.io.InvalidClassException: com.nagarjuna.A; local class incompatible: stream classdesc serialVersionUID = -2586413528665436440, local class serialVersionUID = -1225490151988851177
    at java.io.ObjectStreamClass.initNonProxy(Unknown Source)
    at java.io.ObjectInputStream.readNonProxyDesc(Unknown Source)
    at java.io.ObjectInputStream.readClassDesc(Unknown Source)
    at java.io.ObjectInputStream.readOrdinaryObject(Unknown Source)
    at java.io.ObjectInputStream.readObject0(Unknown Source)
    at java.io.ObjectInputStream.readObject(Unknown Source)
    at com.n1.ExternizationDemo.main(DeserailizationDemo.java:15)






for this reason we have to use SerialVersionUID, From the above example remove comment at serialVersionUID in class 'A', and try  again. You won't get errors, because we mentioned that class A is of serialVersionUID=11L, and nothing changed in code.



source : http://shivasoft.in/blog/java/explain-serialversionuid-in-java













No comments:

Post a Comment