1
  1. Why serialVersionUID must be declared as static, final and of type long variable?
  2. Does this serialVersionUID be unique?
  3. What happens when two or more classes contains the same serialVersionUID? How will this versionUID be useful in this case to deserialize the byte array?
Community
  • 1
  • 1
  • 2
    You have 3 questions in one. Please focus on a single one. If the first, what kind of answers are you looking for? It must be static final and of type long [because the spec says so](http://stackoverflow.com/questions/285793/what-is-a-serialversionuid-and-why-should-i-use-it). – Tunaki Jul 06 '16 at 18:53

2 Answers2

1

It's static because it belongs to the class, not to an instance.

It's final because it doesn't change (for that version of the class). It only changes when someone makes a change to the class that affects how it is serialized.

It's a long because the data type is easy to serialize, fits in a compact space, and has many more values than anyone is likely to need.

It doesn't have to be unique. If two versions of a class have the same serialversionUID that means there are no changes to the class that impact how it's serialized, those two versions of the class can accept the same serialization format.

It doesn't matter if two classes have the same serialVersionUID, it's not used between classes, it's only used to check whether the serialized version of the class is compatible with that version of the class.

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • Why is a long more easier to serialize? Why shouln't it be an int or short something like this? Can you help me knowing this please.. –  Jul 06 '16 at 19:00
  • 1
    @user1391703: this is getting into "because the spec says so" territory. also see Eric Lippert's excellent response to http://meta.stackoverflow.com/questions/323334/is-asking-why-on-language-specifications-still-considered-as-primarily-opinio – Nathan Hughes Jul 06 '16 at 19:01
  • What are figured numbers? Just googled to know about the same but couldn't get any idea.. Please explain me on this.. –  Jul 06 '16 at 19:31
  • @Rabbit 'It could be *that* they figured *that* numbers would be aligned on double-word boundaries', although as this isn't true in Serialization it's all irrelevant. – user207421 Jul 06 '16 at 20:38
1
  1. Why serialVersionUID must be declared as static, final and of type long?

Because the specification says so.

  1. Does serialVersionUID need to be unique?

No. It only differentiates between different versions of your class (e.g. compiled at different points in time). If you're writing a new class you can set serialVersionUID = 1L; without any problems whatsoever.

  1. What happens when two or more classes contains the same serialVersionUID? How will the serialVersionUID be useful in this case to deserialize the byte array?

Two or more classes can declare the same serialVersionUID. The actual wire format contains the fully-qualified class name so there is no ambiguity.

If the serialVersionUID in the data being loaded does not match the serialVersionUID of your class, an InvalidClassException will be thrown.

Daniel Pryden
  • 59,486
  • 16
  • 97
  • 135