Fully explain the serialVersionUID in Java

  • 2020-04-01 02:13:17
  • OfStack

SerialVersionUID role:
In order to maintain version compatibility during serialization, the deserialization maintains the uniqueness of the object during version upgrade.

There are two types of generation:
One is the default 1L, for example: private static final long serialVersionUID = 1L;
One is to generate a 64-bit hash field based on the class name, interface name, member methods, properties, etc., such as:
Private static final    Long        SerialVersionUID = xxxxL;

When you have a class that implements the Serializable interface, Eclipse provides this if the serialVersionUID is not defined
The prompt tells you to define. Click the icon of warning in the class in Eclipse, and Eclipse will
Automatically given two ways of generating. If you don't want to define it, it's also in Eclipse Settings
You can turn it off with the following Settings:
The Window = = > The Preferences = = > Java = = > The Compiler = = > The Error/Warnings = = >
Potential programming problems
Change the warning of Serializable class without serialVersionUID to ignore.

If you don't think about compatibility, turn it off, but it's good to have this feature, as long as any category implements the Serializable interface, Eclipse will give you a warning if you don't add the serialVersionUID, which makes the category Serializable backward compatible.

If your class Serialized is stored on the hard drive, but later you change the field of the class (add or subtract or rename), you will get an Exception when you Deserialize, which can cause incompatibility problems.

But when the serialVersionUID is the same, it will be different fields with the default value of type, Deserialize, to avoid incompatibility problems.


Related articles: