Social Media

SerialVersionUID

The SerialVersionUID is used as part of Serialization/de-Serialization. Its purpose is to ensure that when the object will not be deserialized if the SerialVersionUID’s dont match. If the ID’s dont match then a java.io.InvalidClassException is thrown.

The SerialVersionUID must be static, final, long and preferably private – eg private static final long serialVersionUID = 1L;

It can be generated by –

  • User – You will often see code specified with 1L, 2L. This approach allows the developer to track changes in code like adding a new field
  • Util – IDE or the JDK’s serialver util
  • Compiler – There is also the option to leave it to the compiler to add this value, but this adds an overhead to the code so should be avoided

The key to using any of these approaches is to be consistent, and be sure to update it as the class changes

References

http://stackoverflow.com/questions/888335/why-generate-long-serialversionuid-instead-of-a-simple-1l

About the Author Martin Farrell

My name is Martin Farrell. I have almost 20 years Java experience. I specialize inthe Spring Framework and JEE. I’ve consulted to a range of businesses, and have provide Java and Spring mentoring and training. You can learn more at About or on my consultancy website Glendevon Software

follow me on:

Leave a Comment: