wood burning stoves*
The moose likes Distributed Java and the fly likes serialVersionUID - does it matter what value you choose? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Distributed Java
Bookmark "serialVersionUID - does it matter what value you choose?" Watch "serialVersionUID - does it matter what value you choose?" New topic
Author

serialVersionUID - does it matter what value you choose?

Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

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?


SCJP (1.4 | 5.0), OCJP (6.0), OCMJD
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 16012
    
  19

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.


Customer surveys are for companies who didn't pay proper attention to begin with.
Sean Keane
Ranch Hand

Joined: Nov 03, 2010
Posts: 581

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: serialVersionUID - does it matter what value you choose?