Version Control in Java Serialization – serialVersionUID
Aug 6th, 2009 | By admin | Category: JavaIn my last post on java object serialization , I have explained how to write an object to an object stream and how to read the object state under a different java virtual machine. Lets assume, we have created a class and instantiated it and serialized(Written the state to the file system) it too. Now , we have updated the class file(Added a field to the class) and trying to read the state of the saved object. In this scenario, the class version of the stored object and the class version of the modified class are different. Hence we would get an exception – the java.io.InvalidClassException. This problem is called as versioning Problem.
It happens because all the serialization-capable classes are given a unique identifier. If the identifier of the class is not same as the identifier of the stored(saved) object, the exception will be thrown. This unique identifier for all the classes is maintained in a field named , serialVersionUID. If you want to get rid of the versioning problem , you need to add the unique identifier into your class and give it a some unique value.This would always ensure that the class version and the saved object version are always the same , irrespective of changes you make in the class file.
At this point the question could be , how to add the unique value to the variable serialVersionUID. Don’t worry , whenever you create a class which implements java.io.serializable marker interface , the class become serialization-capable class. Java compiler assigns a unique identifier against each serialization-capable class. you can get the identifier values in the following way.
JDK distribution comes with a utility called serialver which helps you to get the identifier value. Set the JAVA_HOME variable with the correct JDK path.Open the command prompt and type the following and press Enter key,
serialver -show command
Pressing Enter key should popup an input window which would ask you to provide the class name to get the identifier value. Two very important things to remember here .
1. You should provide fully qualified class name.
2. your class name should not have the .class extension (ex – com.etechguide.java.UserObject).
Provide the class name and press the Show button. An id should be populated against Serial Version field.
The popup window should look like,
serialver popup window
Once you get the serial version key(unique Identifier) , copy and paste it in you java class file as a part of your code.
code snippet
If you are coding in the Eclipse IDE , as soon as you create a class which implements java.io.Serializable , you would get an warning ,The serializable class UserData does not declare a static final serialVersionUID field of type long. It means the compiler warns you to add the unique identifier to get rid of the versioning problems.
It is worth to mention that the version will work as long as changes are compatible. Example of a compatible change is , adding a field to the class. At the same time an incompatible change is changing the object’s hierarchy or deleting fields. You can get the whole list of compatible and incompatible changes here.
