Do I need to put a serialVersionUID
on ancestors of a Serializable
class? Or just on the Serializable
class itself?
-
1each class in the hierarchy does need it. – bestsss May 10 '11 at 21:32
-
1@skaffman @bestsss only the Serializable classes need it, not any non-Serializable ancestors. – user207421 May 12 '11 at 00:38
1 Answers
You do not need to put a serialVersionUID at all.
Java will automatically determine an appropriate serial id for your classes based on fields, methods, etc.
The only appropriate time to have serialVersionUIDs is when you will be storing your objects into a file and will need to load them later, even after you have changed your code.
Or, if you will be transferring data across the wire between components that are using different versions of your library.
90% of the time that people need serialization, it is real-time, and library version matching on both sides is guaranteed. In these cases, you do not need to set a serialVersionUID, and it can actually cause problems, if you have two library versions that are incompatible but you have not changed the sID. You will start getting weird errors since Java thinks they're serially compatible, when actually, they're not.
Look at these questions as well:
Is there a reason to use a real serialVersionUID?
Why generate long serialVersionUID instead of a simple 1L?
What is a serialVersionUID and why should I use it?
Edit to add:
As for the original question, no you don't need to worry about your ancestors. Just the serialVersionUID will tell Java if its compatible.
However, if the ancestor changed, and your serialVersionUID did not change, it may not be compatible even though you are saying it is. Another reason why you shouldn't be setting it at all.

- 1
- 1

- 39,701
- 6
- 59
- 77
-
2I think you missed the point of the question. It's not about whether or not you should use `serialVersionUID`, but if you *were* to use it, then should it go in the superclasses as well as the subclass? – skaffman May 10 '11 at 21:41
-
Well, he did ask, and I quote, "Or just on the Serializable class itself?" His premise is wrong due to a lot of bad information being passed around over the years, so I'm correcting that. – Reverend Gonzo May 10 '11 at 21:44
-
1In case you didn't notice, the accepted and highest voted answer in your last link actually is the opposite of your answer. It recommends (the actual javadoc for Serializable) that you always declare one. The first and second links are a different but related issue. – Robin May 10 '11 at 21:56
-
It's exactly what I say with a different opinion. The only use of the ID is for backwards compatibility. Robin thinks it's better to set a number and remember to update it every time. I say, if backwards compatibility isn't needed, don't bother. If backwards compatibility is important, by all means, set the id. The links are there for additional reading to wipe out the bad information that's in people's heads. @Robin I just realized you're the same Robin. I agree with everything you say. I just don't think it's necessary to add an id in most peoples needs. – Reverend Gonzo May 10 '11 at 22:00
-
Let me add, I understand the docs say that it's recommended and they say the reason that it's recommended is that different compilers do different things. Once again, most of the time people need a Serializable object is for things like Remoting and interprocess calls, where they are in full control of the build process for all communicating components, and all components are built together (and probably use the same jars as well). Why add more work maintaining ids you don't need? – Reverend Gonzo May 10 '11 at 22:03
-
Necro-posting, whatever. Answer is still relevant today. Good answer, but some people like me are using serialization to store only the STATE of objects for later use in the application. Things like positions on the screen, etc. Because of this, having an automatically changing serialVersionUID with every tiny method change is REALLY FREAKING ANNOYING because it constantly breaks my save data that will still work perfectly regardless. Now I just have to remember to change the version if I change variables... – Zizzyzizzy Aug 17 '22 at 02:50