47

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???

asked Aug 25, 2011 at 8:33
6
  • 6
    See here: stackoverflow.com/questions/285793/… Commented Aug 25, 2011 at 8:35
  • 3
    This is a good, well-formulated question IMHO, but it has already been answered (mostly) in the link above. As for whether it is limited to Exception, the answer is no, it can be used in any class. Commented Aug 25, 2011 at 8:39
  • 1
    in exception we dont have serializable... then why its there??? Commented Aug 25, 2011 at 8:46
  • 3
    @amod0017 java.lang.Exception implements Serializable, so any subclasses will also implement Serializable. Commented Aug 25, 2011 at 8:49
  • thanks Tom for the information. Commented Aug 25, 2011 at 9:09

3 Answers 3

46

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.

answered Aug 25, 2011 at 8:45
Sign up to request clarification or add additional context in comments.

6 Comments

Well explained. And +1 for the example.
how does serialVersionUID avoid the issue exactly?
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.
you didn't connect it with "why it is normally there in Exception classes.". You should answer the whole question.
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?
|
9

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

answered Aug 25, 2011 at 8:37

Comments

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.3k3 gold badges37 silver badges53 bronze badges
answered Aug 25, 2011 at 8:38

1 Comment

This is not strictly correct. An object is serializable if it, or one of its super classes, implements serializable.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.