It's well known that runtime will genereate serialVersionUID field for your class, if you failed to explicitly define it.
But is it possible to get this value in runitme? Is it possible in runtime, having reference to .class, obtain it's serialVersionUID?
SerialVersionUID is a unique identifier for each class, JVM uses it to compare the versions of the class ensuring that the same class was used during Serialization is loaded during Deserialization. Specifying one gives more control, though JVM does generate one if you don't specify.
Technically you can't prevent two classes from having the same serial version UID, much like you can't prevent two objects from having the same system hash code.
The serialization at runtime associates with each serializable class a version number called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization.
Integer i = new Integer(5);
long serialVersionID = ObjectStreamClass.lookup(i.getClass()).getSerialVersionUID();
Above is the sample code to get serial version id of the class at runtime.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With