There are 3 ways to define the serialVersionUID :
1. private static final long serialVersionUID = 1L; (Default)
2. private static final long serialVersionUID = -
Imagine you write a class with a serialVersionUID
, instantiate it, then serialize it to a file (with ObjectOutputStream)
Then you modify the class.
Then, with the modified class, you deserialize (read in) the version you serialized before modification. Java will check the serialVersionUID
of the current class and the serialized class, and if they don't match, the deserialization will fail. This is a deliberate fail-fast to prevent much more subtle (and harder to debug) errors occurring later on due to class version incompatibilities.
If you omit serialVersionUID
then you disable the version checking.
If you always set if to 1L, then the check will always pass (the value is always the same) so you are still vulnerable to subtle class version incompatibility problems.