I don't know exactly where to use default serialVersionUID or generated serialVersionUID in java ?
private static final long serialVersionUID = 4125965356358329466L; // generated
private static final long serialVersionUID = 1L; // default
The Serial Version ID is used in serializing and deserializing an object. Java identifies if the bytes you want to deserialize match the local class version,if not it will throw an exception. This is important when doing RMI or persisting object structures. The serialization 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.
If you do not provide a serialVersionUID
yourself for a Serializable
class, then Java generates one for you based on the details of that class. Some IDEs and other tools can perform the same computation to enable you to explicitly put that generated serialVersionUID
into your class. It is useful to do this if you want to make changes to the class while maintaining serialization compatibility with the old version -- a somewhat tricky endeavor in most cases, but often doable.
If you're managing the serialVersionUID
manually, on the other hand, then it is a lot easier to use an ordinary serial number, starting at 1 (or some other number you choose) and being incremented whenever the class is changed in a way that breaks serialization compatibility. If you're going to bother with this, though, then do make sure to do it properly -- that is, change the serialVersionUID
when appropriate -- otherwise you defeat the whole purpose.
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