Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine a proper serial version ID?

How to determine a proper serial version ID? Should it be any big long value using some generators or any integer wld suffice ?

like image 315
Rig Veda Avatar asked Jun 12 '09 09:06

Rig Veda


2 Answers

Frankly, as long as the serialVersionUID differs between the different versions of your class, that's all that matters.

What would be bad is if there are two versions of the same class with differing serializable fields having the same serialVersionUID -- that's probably going to cause a problem when performing serialization.

Additionally, if the changes to the class will not affect serialization (e.g. serializable fields in the class remain the same), then the serialVersionUID can remain the same.

IDEs like Eclipse will automatically generate an ID for classes which implement Serializable based on the fields and other information regarding the class, so that may be the easiest route to take to generate an unique ID.

For further reading on the topic, the Discover the secrets of the Java Serialization API would be a good read. There is a section called "Version Control" which discusses the serialVersionUID issue.

like image 74
coobird Avatar answered Oct 31 '22 19:10

coobird


The serialver tool comes with Sun's Java Development Kit (JDK). It takes a full class name on the command line and returns the serial version ID for that compiled class, or can be run with the "-show" parameter to launch a small interactive GUI.

So if your class is Foo, run

serialver Foo

and you'll get some sort of output like this:

Foo: static final long serialVersionUID = -6618469841127325812L;

Take the code starting with "static" and place it inside your class with other static variables. Now the serial version ID is locked in the class.

like image 45
SuPra Avatar answered Oct 31 '22 17:10

SuPra