Same version id for all classes?

Java specification features that

Serialization of serialization is associated with each serializable class, a version number called serialVersionUID, which is used during deserialization to verify that the sender and receiver of the serialized object loaded classes for that object that are compatible with regards to serialization. If the receiver has loaded the class for an object with a different serialVersionUID than the corresponding sender class, then deserialization will result in an InvalidClassException.enter code here

But if I assign all classes the same serial version identifier as follows

static final long serialVersionUID = 1L;

Now all my classes will have the same serialversionUID and this will never throw an InvalidclassException.

I know that we give it explicitly so that its value remains constant in different JVM implementations.

Please let me know why to use the same identifier for all classes and what happens if we change the class between serialization and deserialization?

+4
source share
1 answer

I believe that it is serialVersionUIDused only to determine the version of this class that has been serialized - the class name is always present, so it will not lead to any ambiguity.

Please let me know why use the same identifier for all classes.

They are actually unrelated values. It is just easy to use 1 and increment it every time you make a violation.

, ?

. , - . - , . , , , , - . .

, , , . ( , , protobuf - .)

+3

All Articles