• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

serialVersionUID ??

 
Suren Babu
Greenhorn
Posts: 27
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What is serialVersionUID field? Why it is required?. Where it is used? Is it required for all classes?
 
Michael Ernest
High Plains Drifter
Sheriff
Posts: 7292
Netbeans IDE VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The serialversionUID field holds a unique number that identifies the class' current development. The idea is to maintain a field that can be used to check where a serialized object is based on the same class that the current VM has loaded.
It's not a required field, but it can be very helpful. It's only usable for classes that implement Serializable.
------------------
Michael Ernest, co-author of: The Complete Java 2 Certification Study Guide
 
Brett Spell
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Suren,
The serialVersionUID allows you to explicitly specify the value of something called a stream unique identifier (SUID) that's normally calculated by Java's serialization mechanism. You're never required to define the serialVersionUID field, but you may find it useful sometimes. The SUID value (either one you explicitly define or one that's calculated at run time) of a class is included with the stream of bytes that's written when you serialize / marshall an instance of that class. Then, when that byte stream is being deserialized / unmarshalled, the SUID value in the byte stream is compared with the one for the local class definition to make sure that the two are identical and therefore, compatible.
I can't provide you with more information on how and when to use serialVersionUID without writing a very long response. However, if I haven't fully answered your questions (or even if I have ), I'd encourage you to consider picking up a copy of my book. It includes a chapter on data persistence where I discuss serialization in general and SUIDs in particular in much greater detail.
------------------
Brett Spell
Author, Professional Java Programming
 
Bug Menot
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
since GenericServlet implements Serializable and most people agree that it is a good practice to declare serialVersionUID for classes that implements Serializable, would it also be a good practice to hardcode serialVersionUID in a servlet?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic