Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

what is the different between default and generated serial version uid in java?

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
like image 523
Savan Javia Avatar asked Feb 02 '17 07:02

Savan Javia


2 Answers

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.

like image 54
rushang patel Avatar answered Oct 13 '22 19:10

rushang patel


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.

like image 32
John Bollinger Avatar answered Oct 13 '22 20:10

John Bollinger