This week's book giveaway is in the OO, Patterns, UML and Refactoring forum.
We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line!
See this thread for details.
The moose likes I/O and Streams and the fly likes serialization question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

JavaRanch » Java Forums » Java » I/O and Streams
Bookmark "serialization question" Watch "serialization question" New topic

serialization question

Emanuel Mensa
Ranch Hand

Joined: Feb 22, 2013
Posts: 31
now i understand what it means to impletement Serialization interface. but i have a question. someone did this:

does anyone have an idea what serialVersionUID is? is this some standard thing? what's that number?

that's the whole questions. thanks for any efforts to explain this to me
Matthew Brown

Joined: Apr 06, 2010
Posts: 4541

This is what the documentation for says:

The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long:

ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;

If a serializable class does not explicitly declare a serialVersionUID, then the serialization runtime will calculate a default serialVersionUID value for that class based on various aspects of the class, as described in the Java(TM) Object Serialization Specification. However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during deserialization. Therefore, to guarantee a consistent serialVersionUID value across different java compiler implementations, a serializable class must declare an explicit serialVersionUID value.

Does that make sense? So it gives you a way of versioning serialized objects. Otherwise an attempt to deserialize an object into a class where the definition has changed might cause unexpected results.
Emanuel Mensa
Ranch Hand

Joined: Feb 22, 2013
Posts: 31
yes it makes sense .

so then it means i have to declare my own version number. so i can choose anything random say "899L" right?
Tony Docherty

Joined: Aug 07, 2007
Posts: 2724
Yes you can choose any number of type long that you like or you can generate a number using the serialver tool that comes with the jdk. You run it from the command line eg
I agree. Here's the link:
subject: serialization question
jQuery in Action, 3rd edition