Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SerialVersionUID for Serializable

I am implementing more classes which extend the Serializable interface. I understood it is good to mention a value for serialVersionUID.

private static final long serialVersionUID = 1024L;

So, given that i will use all those classes, should i give the same value for serialVersionUID for each class, or on the contrary, they have to be different?

Thank you.

like image 364
Madrugada Avatar asked Feb 22 '26 23:02

Madrugada


2 Answers

They do not have to be different and also do not have to be the same. It is only used to ensure that some serialized object can be deserialized by that class. Basically, if you changed the code of a serialized class you should also change this value.

like image 146
Lucas Avatar answered Feb 25 '26 11:02

Lucas


The serialVersionUID for one class has absolutely no relation to that of a different class. A corollary is that the serialVersionUID of one class has absolutely no effect on that of a different class.

Thus, you can use the same one for all classes if you wish, and update them all in bulk when any of your classes changes. You should understand the implications it would have to do so.

like image 38
Mark Peters Avatar answered Feb 25 '26 11:02

Mark Peters