Why would the Java compiler create a serialVersionUID synthetic field?
As part of debugging an application, I noticed that Field.getDeclaredFields()
returns some synthetic fields, including a serialVersionUID
field in a class extending an interface, although none extend Serializable
.
Why does the compiler add such fields?
开发者_如何学GoUPDATE
In fact, there is also a $VRc
synthetic field created.
The Java compiler/runtime will not automatically create a serialVersionUID field. I suspect that you are using some form of bytecode enchancement framework under the hood that is being instructed to add the synthetic fields either at runtime, or during compilation.
The $VRc
field is produced by the Emma instrumentation framework, so that would be the reason for at least one of the synthetic fields.
The serialVersionUID
field is also added by Emma, when the instr.do_suid_compensation
property is set to true.
This field is essential for Java serialization. In short: it allows the JVM to discover that the class that was serialized (e.g. saved on disk) has been changed afterwards and cannot be safely deserialized back to object.
Have a look at Version Control chapter in the document quoted above, it explains how serialVersionUID
is used.
UPDATE: just noticed your class does not implement Serializable
. Are you sure none of super classes or implemented interfaces aren't extending Serializable
?
精彩评论