• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

serialVersionUID - does it matter what value you choose?

 
Ranch Hand
Posts: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Saloon Keeper
Posts: 27764
196
Android Eclipse IDE Tomcat Server Redhat Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 590
Eclipse IDE Chrome Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Liar, liar, pants on fire! refreshing plug:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic