Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

serialVersionUID - does it matter what value you choose?

 
Sean Keane
Ranch Hand
Posts: 582
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A quick question I was wondering if anyone had an answer to. When setting the serialVersionUID in a class is there any benefit to setting it to any particular value?

You can generate a value for the serialVersionUID. IDE's like Eclipse will do this for you. Where the generated value is based on the structure of the class just like Java will do at runtime if you don't set the serialVersionUID value.

But why bother doing this? Why not just assign 1 to the first version of the class? Then increment this value by 1 each time you want to change it?
 
Tim Holloway
Saloon Keeper
Pie
Posts: 18164
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It doesn't matter at all. Typically, you would go 1, 2, 3, ..., but there are reasons why you might not. For example, when developing objects in a distributed environment where there's no central authority, you might use something similar to a UUID generator to hand out non-conflicting IDs to different participants.

The Eclipse IDE has support for both methods, but of course you don't need any IDE at all to manage serialVersionIDs. The main point of the serialVersionId is to ensure that when an application marshals an object that the serialized object actually conforms to the same architecture as was expected.
 
Sean Keane
Ranch Hand
Posts: 582
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tim Holloway wrote:but there are reasons why you might not. For example, when developing objects in a distributed environment where there's no central authority, you might use something similar to a UUID generator to hand out non-conflicting IDs to different participants.


Ah, that scenario makes the use of a generated value make sense! Thanks.

I'm working on the the OCMJD certification. So I think I will just stick with assigning the value 1 to each serialVersionID. Should be good enough.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic