45

Possible Duplicate:
Why should I bother about serialVersionUID?

I am going through some exception handling code and i saw something named as serialVersionUID. What this uid is for?? Is it only limited to exception or can be used in all classes??? what is the advantage of this id???

Community
  • 1
  • 1
amod
  • 4,190
  • 10
  • 49
  • 75

3 Answers3

45

serialVersionUID is a field to define the version of a particular class while seriializing & deseriializing.. consider a scenario where you have a class Employee which has 3 fields which has been in production for some time (meaning there may exist many serialized versions of employee objects), when you update the class to include (say a 4th field) then all the previous class (which are serialized) cannot be casted or de-serialized to the new one & you'll get an exception.

to avoid this issue, you can use the serialVersionUID field to tell JVM that the new class is in fact of a different version (by changing the serialVersionUID).

@Farmor & @Tom Jefferys said pretty much the same thing, but with an example things look a lot simpler.

surendrapanday
  • 530
  • 3
  • 13
Anantha Sharma
  • 9,920
  • 4
  • 33
  • 35
  • Well explained. And +1 for the example. – Saurabh Sharma Apr 28 '15 at 06:55
  • how does `serialVersionUID` avoid the issue exactly? – typing... Jan 10 '20 at 20:53
  • when deserializing java can only deserialize non-volatile non-constant (non-final) fields. since `serialVersionUID` is both static and final the deserialization will fail because the values of the field in class definition doesn't match the one in serialized object. This way the JVM can throw an exception instead of partially deserializing (or hyderating) an object. – Anantha Sharma Jan 15 '20 at 06:56
  • 2
    you didn't connect it with "why it is normally there in Exception classes.". You should answer the whole question. – saurabh gupta Dec 26 '20 at 14:41
  • Requesting a little clarification: 1. So, if I update my `Employee` class in future, should I change the `serialVersionUID` value? 2. Should the `serialVersionUID` values be different for different classes, eg. for `Employee` and `Client`? – Prasannjeet Singh Mar 14 '22 at 11:04
  • Isn't this metadata, would not it be nicer if handled at annotation level? And what would be the subclasses ? – ᐅdevrimbaris Feb 28 '23 at 07:04
7

It's used for serialization, it should be declared in any class that implements Serializable.

It's effectively a version number the JVM can use to check if a serialized class matches the class definition you're trying to deserialize it into.

There's more information on it here: http://download.oracle.com/javase/1,5.0/docs/api/java/io/Serializable.html

Tom Jefferys
  • 13,090
  • 2
  • 35
  • 36
3

It's to determine if they have serializable and deserializable compatibility if they have the same serialVersionUID and both implements Serializable then they are compatible.

And it's not limited to exceptions only as you will notice eclipse is prone to put a serialVersionUID early in your normal java class if it implements Serializable.

Edited: Updated to include @Spychos correct comment about the Serializable interface.

dsh
  • 12,037
  • 3
  • 33
  • 51
  • 1
    This is not strictly correct. An object is serializable if it, or one of its super classes, implements serializable. – Spycho Aug 25 '11 at 08:40