-1

Possible Duplicate:
Serialization version uid in Java

We know that Java serialization mechanism ignore static fields. If it is this, then how the deserializer knows the serialVersionUID of the serializer. serialVersionUID is always a static field of the class to be serialized. Thank you for your help.

cheng

Community
  • 1
  • 1
cheng
  • 2,106
  • 6
  • 28
  • 36
  • @jalopaba Thank you for your reminding. Maybe there are ways to merge the two questions. – cheng Jun 12 '12 at 08:36
  • The weird thing is I got a downvote in my answer in the other question :-) – jalopaba Jun 12 '12 at 08:38
  • Too bad... I found your answer useful and vote it up. – cheng Jun 12 '12 at 08:43
  • "We know that Java serialization mechanism ignore static fields." No we don't. We know that it *doesn't serialize* static fields. Not the same thing. And *obviously* the `serialVersionUID` field gets special treatment. Not a serious question. – user207421 Jun 12 '12 at 10:53

3 Answers3

5

The serializedVersionUID is written to the stream, even though it is static.

It is then used on deserialization to compare it against the one in the currently available version of the class.

Note that it is not written as part of every instance, but as part of the "ObjectStreamClass" that defines the class for all instances in the stream that want to use it.

Thilo
  • 257,207
  • 101
  • 511
  • 656
2

The Class that is deserialized is in the Classpath so all that data is known.

Update (thanks to @Thilo):

"and equally importantly, the serializedVersionUID is written to the stream, even though it is static." – Thilo
ssedano
  • 8,322
  • 9
  • 60
  • 98
0

serialVersionUID is retrieved from the class not stored instance of a "serialized" object.it is "stored" in the compiled bytecode if it is defined otherwise it is computed using some algo.

Refer below byte code of class that implements Serializable interface.

amicngh
  • 7,831
  • 3
  • 35
  • 54