Sometimes when we write code, if we don’t write a class that needs serialization
serialVersionUID, the compiler may prompt us
The 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 have
serialVersionUID。 So what exactly is serialVersionUID? What’s the usage?
Attached official document connection:https://docs.oracle.com/javas…
What is it?
serialVersionUIDIs a version number used for verification during serialization and deserialization.
The serialization runtime willVersion number(referred to as
serialVersionUID）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 class
serialVersionIdDifferent, deserialization will result in
Serializable classes can be created by declaring
serialVersionUIDFields that explicitly declare their own
serialVersionUID, and the field must be
finalAnd of type
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 explicitstatement
serialVersionUID, the serialization runtime will calculate the default value of the class based on various aspects of the class
But,Strongly recommendedExplicit declaration of all serializable classes
serialVersionUIDValue because the default
serialVersionUIDCalculations are highly sensitive to class details, which may vary depending on the compiler implementation, so unexpected errors may occur during deserialization
Therefore, in order to ensure that in different java compiler implementations
SerialVersionIdValues are consistent, and serializable classes must declare an explicit
SerialVersionIdValue. stillStrongly recommendedExplicit
serialVersionUIDStatements should be used whenever possible
privateModifier because this type of declaration applies only to classes declared immediately——
serialVersionUIDField is not available as an inherited member.
Q: if the parent class is serialized, the child class is serialized by default, so we also need to declare for child
A: it is recommended that subclasses, or every class with serialization requirements, be
serialVersionUIDAnd as recommended above, use
privateModify 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 declared
Welcome to pay attention and make progress together~
send outSpringBootGet springboot information package