views:

426

answers:

3

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

+1  A: 

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.

Suvesh Pratapa
+4  A: 

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.

coobird
+2  A: 

Possible uses:

  • version - increment it when the class changes in an incompatible way;
  • timestamp - set it to the current timestamp - e.g. 200906121213 when the class changes in an incompatible way.
Robert Munteanu
what does your second point mean ? setting it to timestamo makes it incompatible? or is it not proper practice.
Rig Veda
Sorry : then -> when . Will fix.
Robert Munteanu