Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
+1 vote
2 views
in Java by (1.1k points)

Eclipse issues warnings when a serialVersionUID is missing.

The serializable class Foo does not declare a static final serialVersionUID field of type long

What is serialVersionUID and why is it important? Please show an example where missing serialVersionUID will cause a problem.

1 Answer

0 votes
by (13.2k points)

Serialization is described as a process in which the conversion of state of the object into byte stream takes place, while deserialization is the reverse process in which byte converts to object.

 

Now, the serialization at runtime attaches with each serializable class a version number which is referred as serialVersionUID. This identity is used during deserialization to verify that the receiver and sender of a serialized object have loaded the classes for that object which is compatible with serialization. So the use of serialVersionUID is to make sure during deserialization, that the same class is in fact loaded. 

Coming on to the last part of your question as to the effect of wrong serialVersionUID, thing to understand is UID is generated automatically based on a few factors namely class name, implemented interfaces, all its public, protected members. If any of them are changed it will change the serialVersionUID. During deserialization, the receiver side JVM compares the UID associated with the object to the local class UID. If both unique UIDs match then only deserialization will be performed. Otherwise we will the error in form of InvalidClassException.

Related questions

0 votes
1 answer
0 votes
1 answer
0 votes
2 answers

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...