What does serialVersionUID do?

Time:2021-9-14

Sometimes when we write code, if we don’t write a class that needs serializationserialVersionUID, the compiler may prompt usThe serializable class ClassName does not declare a static final serialVersionUID field of type long

Students who have used the mybatis plus framework should also find that when using reverse code generation, the generated entity classes also havestatic finalModifiedlongtypeserialVersionUID。 So what exactly is serialVersionUID? What’s the usage?

Attached official document connection:https://docs.oracle.com/javas…

What is it?

In short,serialVersionUIDIs a version number used for verification during serialization and deserialization.

The serialization runtime willVersion number(referred to asserialVersionUID)Associated with each serializable class, the version number isDuring deserialization, it is used to verify whether the sender and receiver of the serialized object have loaded serialization compatible classes for the object

If the class loaded by the receiver for the object is different from that of the corresponding sender classserialVersionIdDifferent, deserialization will result inInvalidClassException

Serializable classes can be created by declaringserialVersionUIDFields that explicitly declare their ownserialVersionUID, and the field must bestaticfinalAnd of typelong

ANY-ACCESS-MODIFIER static final long serialVersionUID=42L;

What happens if you don’t declare?

As described in the Java (TM) object serialization specification, if a class can be serializedNo explicitstatementserialVersionUID, the serialization runtime will calculate the default value of the class based on various aspects of the classserialVersionUIDValue.

But,Strongly recommendedExplicit declaration of all serializable classesserialVersionUIDValue because the defaultserialVersionUIDCalculations are highly sensitive to class details, which may vary depending on the compiler implementation, so unexpected errors may occur during deserializationInvalidClassExceptions

Therefore, in order to ensure that in different java compiler implementationsSerialVersionIdValues are consistent, and serializable classes must declare an explicitSerialVersionIdValue. stillStrongly recommendedExplicitserialVersionUIDStatements should be used whenever possibleprivateModifier because this type of declaration applies only to classes declared immediately——serialVersionUIDField is not available as an inherited member.

Other issues

Q: if the parent class is serialized, the child class is serialized by default, so we also need to declare for childserialVersionUIDAre you?

A: it is recommended that subclasses, or every class with serialization requirements, beserialVersionUIDAnd as recommended above, useprivateModify to avoid the protected inheritance of the child class from the parent class (I haven’t encountered the situation of blowing hair yet, so it’s hard to tell what problems will occur under what circumstances after inheritance)

Q: do I need to specify if I don’t serialize?

A: if there is no serialization requirement, there is no comparison between serialization and deserialization. In principle, it is not declaredserialVersionUIDIt’s OK


Welcome to pay attention and make progress together~
send outSpringBootGet springboot information package

What does serialVersionUID do?