Questions tagged [serialversionuid]

`serialVersionUID` is an optional attribute of Java classes to indicate the standard serialization/deserialization format version. Used to detect if a serialized object is incompatible with the deserialization process/class.

Documented in java.io.Serializable:

The serialization runtime associates with each serializable class a version number, called a serialVersionUID, which is used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible with respect to serialization. If the receiver has loaded a class for the object that has a different serialVersionUID than that of the corresponding sender's class, then deserialization will result in an InvalidClassException. A serializable class can declare its own serialVersionUID explicitly by declaring a field named "serialVersionUID" that must be static, final, and of type long:

ANY-ACCESS-MODIFIER static final long serialVersionUID = 42L;
122 questions
3395
votes
25 answers

What is a serialVersionUID and why should I use it?

Eclipse issues warnings when a serialVersionUID is missing. The serializable class Foo does not declare a static final serialVersionUID field of type long What is serialVersionUID and why is it important? Please show an example where missing…
ashokgelal
  • 80,002
  • 26
  • 71
  • 84
248
votes
5 answers

What does it mean: The serializable class does not declare a static final serialVersionUID field?

I have the warning message given in the title. I would like to understand and remove it. I found already some answers on this question but I do not understand these answers because of an overload with technical terms. Is it possible to explain this…
Roman
  • 124,451
  • 167
  • 349
  • 456
228
votes
10 answers

Why generate long serialVersionUID instead of a simple 1L?

When class implements Serializable in Eclipse, I have two options: add default serialVersionUID(1L) or generated serialVersionUID(3567653491060394677L). I think that first one is cooler, but many times I saw people using the second option. Is there…
IAdapter
  • 62,595
  • 73
  • 179
  • 242
77
votes
4 answers

Should an abstract class have a serialVersionUID

In java, if a class implements Serializable but is abstract, should it have a serialVersionUID long declared, or do the subclasses only require that? In this case it is indeed the intention that all the sub classes deal with serialization as the…
Yishai
  • 90,445
  • 31
  • 189
  • 263
76
votes
12 answers

Use the serialVersionUID or suppress warnings?

I want to create a class that, for example, extends HttpServlet? My compiler warns me that my class should have a serialVersionUID. If I know that this object will never be serialized, should I define it or add an annotation to suppress those…
Okami
  • 877
  • 1
  • 9
  • 11
56
votes
3 answers

Why my exception class needs to be serialized?

When you extend a class with class Exception ( for creating new exception) you get a warning to have a serialVersionUID. I know that serialVersionUID plays an important role while serialization and deserialization, but when my Exception needs to be…
amod
  • 4,190
  • 10
  • 49
  • 75
45
votes
3 answers

What is serialVersionUID in java, normally in exception class?

Possible Duplicate: Why should I bother about serialVersionUID? 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???…
amod
  • 4,190
  • 10
  • 49
  • 75
35
votes
2 answers

Getting rid of the comment above Eclipse-generated serialVersionUID

This has become a pet peeve of mine. I write a class, and implement Serializible. Then eclipse warns me that I don't have a serialVersionUID, so I select "Add generated serialVersionUID" or "Add default serialVersionUID" and I end up with something…
mrip
  • 14,913
  • 4
  • 40
  • 58
26
votes
6 answers

explicit serialVersionUID considered harmful?

It seems to me that explicitly specifying serialVersionUID for new classes is bad. Consider the two cases of not changing it when layout has it should have been changed and changing it when it should not have. Not changing when it should have been…
Miserable Variable
  • 28,432
  • 15
  • 72
  • 133
24
votes
9 answers

Finding serialVersionUID of serialized object

Is there a way to determine the generated serialVersionUID of a serialized Java object? The problem is that I serialized an object without explicitely specifying the serialVersionUID. Now the deserialization process complains about class…
paweloque
  • 18,466
  • 26
  • 80
  • 136
22
votes
5 answers

Make Java runtime ignore serialVersionUIDs?

I have to work with a large number of compiled Java classes which didn't explicitly specify a serialVersionUID. Because their UIDs were arbitrarily generated by the compiler, many of the classes which need to be serialized and deserialized end up…
kpozin
  • 25,691
  • 19
  • 57
  • 76
15
votes
3 answers

Eclipse auto-generation of serialVersionUID with each change

Eclipse nicely generates the serialVersionUID for me. But this seems to be passive code generation as the id won't be automatically updated as I change the file unless I do the generation again. Is there some way to have the serialVersionUID being…
Touko
  • 11,359
  • 16
  • 75
  • 105
15
votes
2 answers

serialVersionUID field warning in eclipse

I have just started on AWT and made a simple program in it, it works fine but it shows a warning message in eclipse which i don't understand: The serializable class TestGUI does not declare a static final serialVersionUID field of type long I…
Surender Thakran
  • 664
  • 3
  • 7
  • 21
12
votes
6 answers

How to deserialize an object persisted in a db now when the object has different serialVersionUID

My client has an oracle data base and an object was persisted as a blob field via objOutStream.writeObject, the object now has a different serialVersionUID (even though the object has no change, maybe different jvm version) and when they try to…
Jorge Perez
  • 121
  • 1
  • 1
  • 6
12
votes
1 answer

Is Java SerialVersionUid of 1L ok? Or does it need to be unique?

I have two java classes which implement Serializable. I set both of them to have a serialVersionUid of 1L. A coworker said that all classes must have a unique serial version uid and that the jvm will treat classes as equal if they have the same…
David
  • 123
  • 1
  • 5
1
2 3
8 9