0

I was following the article http://javapapers.com/core-java/serialversionuid-in-java-serialization/ to understand Seriliazation.

In this its said that Java serializationUID recommended to be unique. I dont understand the reason for that.

Can someone please explain?

Vaibhav
  • 2,073
  • 4
  • 23
  • 26

1 Answers1

1

In this its said that Java serializationUID recommended to be unique. I dont understand the reason ...

It's wrong. There is no reason not to use 1L for every class, except in the case where you are retrofitting to a class that was initally defined without one, in which case you have to use what the serialver tool tells you.

As a matter of fact you can't use a unique value for every class, because it's a hashcode.

Don't rely on arbitrary Internet junk like this. Use the Javadoc and the specifications.

user207421
  • 305,947
  • 44
  • 307
  • 483
  • There is a reason, read the documetation if you don't catch what the reason is. – Krzysztof Cichocki May 25 '15 at 11:36
  • @KrzysztofCichocki I've been reading it since 1997. There is no reason other than the one I have stated, and specifically there is no reason why the serialVersionUIDs of two different classes can't be the same, which is what the question is about. If you disagree it is up to you to substantiate your claim. – user207421 May 25 '15 at 11:39
  • How you can use it for all classes, if you would like to change one in version, and you have already serialized it and stored and you need it? Where would you store the previous class? in the void of outsideness of all the classes? If all can have the same numbers, it is bad practice. – Krzysztof Cichocki May 25 '15 at 11:48
  • @KrzysztofCichocki I have no idea what you're talking about. This business about 'void of outsideness' is just meaningless drivel. – user207421 May 25 '15 at 11:54
  • It is the crazy place where according to you, you should store your class which version have changed. – Krzysztof Cichocki May 25 '15 at 11:56
  • @KrzysztofCichocki I won't have words put into my mouth. I have said no such thing, and you know it perfectly well. You're off topic. Tell us the problem with using a non-unique value across more than one class, and then tell us how a hashcode can possibly have unique values over the entire population of classes. – user207421 May 25 '15 at 11:57
  • if all classes should have the same UID, then where you store one with other version? it this question to complicated? – Krzysztof Cichocki May 25 '15 at 11:58
  • @KrzysztofCichockiI (1) I don't agree with the popular misconception that you should change the `serialVersionUID` every time you change the class, but (2) the question and my answer aren't about making it *compulsory* to use the same value forever: they are about whether it is *possible*. Nobody here appears to have actually read the question. – user207421 May 25 '15 at 12:01
  • @KrzysztofCichocki Cut out the personal remarks, and don't make false accusations here. – user207421 May 25 '15 at 12:06