Madrugada
Madrugada

Reputation: 1289

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.

Upvotes: 2

Views: 481

Answers (4)

Mark Peters
Mark Peters

Reputation: 81054

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.

Upvotes: 3

r0ast3d
r0ast3d

Reputation: 2637

Since you have declared it as private this value is unique to this class and it is unique and not an inherited value.

http://docs.oracle.com/javase/1.5.0/docs/api/java/io/Serializable.html

Changing this value to any other value in future in this class will give you an invalidclassexception when you try to deserialize an old object.

Cheers!

Upvotes: 2

Lucas
Lucas

Reputation: 14919

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.

Upvotes: 4

ewan.chalmers
ewan.chalmers

Reputation: 16235

The key thing is that if you later change a class in a matter making the new serialized form incompatible with the original, you must change the serialVersionUID. Apart from that, you can use any value for that field.

Upvotes: 2

Related Questions