Friday, September 28, 2018

serialization - What is serialVersionUID in java, normally in exception class?











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


Answer



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.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...