What is serialVersionUID in java, normally in exception class?

36,578

Solution 1

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.

Solution 2

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

Solution 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.

Share:
36,578
amod
Author by

amod

I am a software developer with strong desire to learn and work on new technologies and solve complex problems with my solid programming knowledge and analytical skills. Currently, I am a Software Developer at Honeywell, Atlanta. I did Master in Computer Science from Lamar University, USA. I had worked as a Senior R&D Engineer in Nokia Networks for 2+ years and before that, I was a software developer in Persistent Systems Ltd for 2 years. In Nokia Networks I was part of core R&D team which design and implement complex telecommunication related algorithms to optimize the mobile network. While working, along with honing my programming skills, I became adept at analytical thinking, debugging, troubleshooting and team working. My core responsibilities also contain guiding junior developer to complete the task provided to them. I like writing code to make day to day life easier. Apart from coding my hobby is to write Blogs I used to write Blogs on various technology available on internet, games, gadgets, social networking, software, Cloud-based applications. I like reading blogs as well, exploring software products and gadgets. I am also a founding member of website CampFestiva.com which publish college events happening all over India. I have been working hard on this for more than 3+ years to make it what it is now. Campfestiva is a non-profitable attempt to provide information about various opportunities to students in India.

Updated on February 19, 2020

Comments

  • amod
    amod about 4 years

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

  • Spycho
    Spycho over 12 years
    This is not strictly correct. An object is serializable if it, or one of its super classes, implements serializable.
  • Saurabh Sharma
    Saurabh Sharma about 9 years
    Well explained. And +1 for the example.
  • typing...
    typing... over 4 years
    how does serialVersionUID avoid the issue exactly?
  • Anantha Sharma
    Anantha Sharma over 4 years
    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.
  • saurabh gupta
    saurabh gupta over 3 years
    you didn't connect it with "why it is normally there in Exception classes.". You should answer the whole question.
  • misteeque
    misteeque about 2 years
    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?