12

I have two java classes which implement Serializable. I set both of them to have a serialVersionUid of 1L.

A coworker said that all classes must have a unique serial version uid and that the jvm will treat classes as equal if they have the same serial version uid. I thought equality was based on the result of the equals method and not the serial version uid.

It was my understanding that the serial version uid was used to indicate the version of the class and that when the class changed in an incompatible fashion that the serial verison uid should be incremented.

Is that correct? Is it okay to use a serialversion uid of 1? Or should java classes never have a serialversion uid of 1L?

David
  • 123
  • 1
  • 5
  • possible duplicate of [What is a serialVersionUID and why should I use it?](http://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it) – K Erlandsson Jun 08 '15 at 18:57
  • The difference is that I specifically want to know about whether 1L is an acceptable serial version uid (not whether a serialverionuid is necessary). Perhaps another way to word my question is 'does the serial version uid need to be unique?' – David Jun 08 '15 at 18:58
  • Have you tried reading those answers? I assure you your question will be answered if you read that thread. – K Erlandsson Jun 08 '15 at 19:05
  • Well, the other person that I work with read the answers to mean that 1L is not an allowed value and I read it to indicate that it is. – David Jun 08 '15 at 19:08
  • Probably one similar question would be http://stackoverflow.com/questions/888335/why-generate-long-serialversionuid-instead-of-a-simple-1l?rq=1 – David Jun 08 '15 at 19:10
  • Ok, I might have been a bit harsh, the question I linked does not answer your question in a very obvious manner. Apologies. – K Erlandsson Jun 08 '15 at 19:12
  • no problem. thanks for looking at the question. – David Jun 08 '15 at 19:12
  • 2
    'The other person that you work with' is dreaming. There is nothing there that says `1L` is an illegal value. There's only one answer there that even mentions it. `serialVersionUID` is a `long,` and *any* value is allowed. – user207421 Jun 09 '15 at 04:11

1 Answers1

18

The class name is part of the serialized representation of an object. The serialVersionUID is only used for versioning the classes. So 1L is a valid value.

Note that if you don't plan to maintain the compatibility of serialization while evolving the class, serialVersionUID is useless, and you can just omit it. Having a serialVersionUID is useful when you want to make compatible changes to a class and still be able to read objects that were serialized using an older version of the class (or vice-versa). But that requires extreme care, and is far from being an easy task. You should generally avoid using serialization for long-term storage. If used for networking purpose, using the same exact classes for the client and the server (i.e. deploying both at once) is the easiest strategy.

Also note that you could easily prove your coworker that he's wrong by simply serializing and deserializing two objects of two different classes having both the same value (1L) as serialVersionUID. If his theory was true, the JVM would have no way of knowing how to deserialize the objects.

JB Nizet
  • 678,734
  • 91
  • 1,224
  • 1,255