3

I came across a exception handler class that extends exception as follows:

public class AppFileReaderException extends Exception {
    //Explicit serialization UID added 
    private static final long serialVersionUID = -2458461415998318236L;

    public AppFileReaderException(String msg) 
    {
        super(msg);
    }

The author used an explicit serialization version UID and has ignored the same in other similar exception handler classes. Based on what I understood from another SO post, we can ignore the serialization UID if the application is not using any serialization and deserialization. Oddly, the author used a negative UID. I am curious to know if that is valid or recommended practice. Is there any other reason to use a negative serialVersionUID?

Community
  • 1
  • 1
Shan
  • 5,054
  • 12
  • 44
  • 58

3 Answers3

12

Is there any other reason to use a negative serialVersionUID?

If you see any "random looking" explicit serialVersionUID, it's probably been generated by an IDE, typically starting off as the UID which would have been generated if an explicit version hadn't been provided.

Serialization version UIDs are like hash codes - the magnitude of the value is effectively irrelevant; positive and negative are meaningless, other than for differentiation between values.

Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
2

The seialVersionUID is used to determine if a serialized object is compatible with the current version of the class. There are no restriction besides that the value has to be a long and that incompatible versions of your class use different values.

mschenk74
  • 3,561
  • 1
  • 21
  • 34
2

java.io.Serializable API explains that serialVersionUID may be calculated or fixed. Calculated value is based on various aspects of the class. It is similar to hashCode which can also be negative. There is serialver util in JDK which given a class can calculate this value, though in practice it is typically generated by IDE.

Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275