0

I'm using the serialization mechanism for save and load objects from the HDD. I think that define the field "serialVersionUID" is redundancy for me.

How can I say JVM doesn't compare these fields?

Exception in thread "main" java.io.InvalidClassException: ClientRepository; local class incompatible: stream classdesc serialVersionUID = -477189107700903771, local class serialVersionUID = 1
Iaroslav Baranov
  • 1,100
  • 9
  • 17
  • You cannot switch of serialVersionUID – ControlAltDel May 08 '15 at 14:19
  • You can't. If you're using serialization for long term storage, which is a really bad idea, you'd better think hard about how you'll make evolutions to your classes without losing the way to load saved previous versions of the class. Read the serialization specifications. Or forget about this bad idea, and use a better format, like JSON or XML, that makes evolutions easier. – JB Nizet May 08 '15 at 14:19

1 Answers1

0

You can add a fixed static long final field serialVersionUID. Then you only need to change it, when the structure changes.

Otherwise the javac compiler calculates a serialVersionUID based on the structure of the class. And adds the field hidden to the class (like the default constructor).

The last method is to implement readObject/writeObject, allowing for instance compatibility between two close version differences, by doing on different versions a conversion.

Joop Eggen
  • 107,315
  • 7
  • 83
  • 138