Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Java in General and the fly likes serialVersionUID ?? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "serialVersionUID ??" Watch "serialVersionUID ??" New topic
Author

serialVersionUID ??

Suren Babu
Greenhorn

Joined: Oct 23, 2000
Posts: 27
What is serialVersionUID field? Why it is required?. Where it is used? Is it required for all classes?
Michael Ernest
High Plains Drifter
Sheriff

Joined: Oct 25, 2000
Posts: 7292

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


Make visible what, without you, might perhaps never have been seen.
- Robert Bresson
Brett Spell
Ranch Hand

Joined: Feb 24, 2001
Posts: 96
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

Joined: Oct 15, 2004
Posts: 26
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?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: serialVersionUID ??