4

On a project, we have several objects serialized. It will be necessary to use these objects on machine with different JVM (possibly different versions).

Our objects serialVersionUID are fixed and won't change, but we are concerned about the serialVersionUID of the JVM standard objects, for instance ArrayList/HashSet that are used in our serialized objects.

So the question is, can these serialVersionUID change between different versions of JVM or between different JVM ?

Or do we have to use another serialization mechanism to support different JVMs ?

Baptiste Wicht
  • 7,472
  • 7
  • 45
  • 110

2 Answers2

3

The serialVersionUID should only be changed if there is a change to the class that would not be compatible with previously serialized versions of it.

To see what changes would potentially break compatibility check the Specification

I highly doubt that a new version of Java would introduce any changes to core classes that would break compatibility.

ivarni
  • 17,658
  • 17
  • 76
  • 92
-1

We use serialVersionUID as a version code for the class, and we should change this field when we modify the class. This field is used as identity of the class in deserialization.

For example, you serialize a object of class A and save it in an binary file, you can deserialize file to the original object later. But if you add a field to A and do not change the serialVersionUID, the deserialization may return a malformed object. And if you change the serialVersionUID, the deserialization will reject the input and throw an exception. An exception is better than a unknown error.

These error/exception happen if and only if you used an old serialization result to create a instance of a modified class. If you don't use serialization for data persistence, there won't be any problems.

faylon
  • 7,360
  • 1
  • 30
  • 28
  • We should *not* 'change the field when we modify the class,' unless we deliberately want to introduce an incompatibility. The ideal is to *never* change it. – user207421 Jul 25 '13 at 11:27
  • @EJP I cannot agree with you. Sometimes i have to break the compatibility if i make some incompatible changes in the class, which will cause some hidden errors in deserialization. I agree that we should not change the structure of class once it uses serialization for data exchange, but once it is changed, we have to change the serialVersionUID. – faylon Jul 25 '13 at 12:03
  • Adding a field will not produce an error... http://docs.oracle.com/javase/7/docs/platform/serialization/spec/version.html#5172 – ivarni Jul 25 '13 at 12:26