Sunday, October 20, 2013

Gererating SerialVersionUID for a serializable class...

Hi All,

In this post I am going to show you how to generate serialVersionUID for a serializable class.(java.io.Serializable)


Purpose of  the serialVersionUID variable : 

Any serializable class will need a unique class identifier called serialVersionUID.

              This long value is used to allow the JVM to know if a version of the class has changed. If you neglect to include it in your class, then one will be generated on the fly. You don’t want that.


Generating serialVersionUId for our custom class : 

  Take an example class A which implements Serializable inteface. 

    public class A implements Serializable{

    } //end class


If we want to generate unique serial version uid, then we have a command in JDK's bin folder with the name serialver.


use below command to generate UID.

$ serialver A
A:    static final long serialVersionUID = -5362330504532103641L;

we can type the above command may times, we will get the same UID all time.


Note: For any library related serializable class  like hibernate or spring related serializable class, while generating UID, all the instance related classes must be in class path. So don't forget to put all compilation related classes in classpath.



No comments:

Post a Comment