Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why use random numbers for serial version UID? [duplicate]

EDIT: By random I mean a large computed number with no semantic meaning to us as developers

When implementing the Serializable interface its best practice and really important to specify a serial version UID. In numerous places, I often see random numbers being used. E.g.

Effective Java (2nd edition) pg 312:

private static final long serialVersionUID = 234098243823485285L;

From the String class in Java 6:

private static final long serialVersionUID = -6849794470754667710L;

From the ArrayList class in Java 6:

private static final long serialVersionUID = 8683452581122892189L;

etc. Even eclipse offers the option to generate these random numbers (though the primary default appears to be to generate a serialVersionUID of 1L)

Why use random numbers? Doesn't it make more sense to start at 1L and increment to 2L when it changes like any sensible revision control? The only time I can think of to use a seemingly random number is if you didn't specify a serialVersionUID to begin with and want to do so now (which ties you in to the run-time autogenerated version to provide backwards compatibility support).

like image 841
Chris Knight Avatar asked Feb 20 '13 23:02

Chris Knight


2 Answers

Those "random" numbers are probably the numbers which would have been generated for the class automatically in its "current" form as per the Java Object Serialization Specification... where "current" is "current at the time serialVersionUID was first declared".

That would allow data which had been previously serialized to still be deserialized - while moving forward to a more explicit declaration of breaking changes in the future.

like image 182
Jon Skeet Avatar answered Oct 18 '22 12:10

Jon Skeet


They are almost certainly not random numbers, but rather the output of the serialver tool.

like image 43
user207421 Avatar answered Oct 18 '22 10:10

user207421