Why do objects have private static final long serialVersionUID = 1L(language-Java)?

Haody_Du 注册会员
2023-02-27 23:46

serialVersionUID: Defined as the version that represents the class definition. During deserialization, the jvm compares the serialVersionUID in the byte stream state class to the serialVersionUID in the local class. If the class is the same, the JVM serializes it, throwing an InvalidClassException if not.

private static final identifies it as a private static constant because it has no real business significance and is used only for serialization and deserialization purposes.

a58438331 注册会员
2023-02-27 23:46

private, static, final
static means that this variable does not depend on the instantiated object and exists as long as the class is loaded into the JVM.
And final means that the variable cannot be modified again.

cuixiao8765 注册会员
2023-02-27 23:46

What this code means is that the value of SerialVersionUID is defined as a constant.
What is the Serializable interface that contains SerialVersionUID?
Serializable: An object serialization interface. A class can serialize its objects only if it implements the Serializable interface. Serializable is a semantic-level interface defined in the Java.io package and provided for implementing serialization operations of Java classes. The Serializable Serialization interface does not have any methods or fields but identifies serializable semantics.

Classes that implement the Serializable interface can be converted to a byte stream by ObjectOutputStream or resolved to an object by ObjectInputStream. For example, we can write a serialized object to a file, read it from the file and deserialize it into an object, that is, recreate the object in memory using type information and bytes representing the object and its data.

What is serialization?
Serialization is the process of converting the state of an object into a format that can be held or transmitted. The opposite of serialization is deserialization, which converts a stream into an object. Combined, these two processes make it easy to store and transfer data. Any type that implements the Serializable interface can be saved to a file or sent as data flow over the network. It can also be piped to other programs in the system.

Why define serialVersionUID to be private, static, final to modify
serialVersionUID is called a stream identifier, which is the version definition of the class and is used to preserve version compatibility during serialization. That is, deserialization maintains the uniqueness of the object when the version is upgraded. The jvm compares the version name when deserializing, and only deserializes if the serialVersionUID in the data stream and the serialVersionUID in the class are the same, but throws an exception if they are different. In general, if you make a change to a class that implements the serializable interface, you need to change this version information. serialVersionUID can be declared either explicitly or implicitly. Implicit declarations are calculated by the package name, class name, etc., while explicit declarations are set by the assignment itself.

If you do not write private static final long serialVersionUID = 1L, when you make changes to this class, if you forget to change the serialVersionUID, there will be incompatibilities on the version. So you get a deserialization error

If you show that you have defined private static final long serialVersionUID = 1L, when making changes to this class, if you forget to change the serialVersionUID, this class can also be deserialized, and it will automatically be upward compatible. No errors will be reported.