Skip to main content
Last updated June 25, 2012 11:59, by Martijn Verburg
Feedicon  

From Stuart Marks:

For the curious, the reason that anonymous inner classes shouldn't have a serialVersionUID is that serializing them might or might not work and that it's not possible to guarantee serialization compatibility for them. An anonymous inner class -- actually any inner class or non-static nested class -- has a reference to an "enclosing instance" of the class within which the inner class is defined. If the enclosing class isn't serializable, serialization of the inner instance will fail. Also, even if it were serializable, serializing the enclosing instance is probably not what you want. In addition, anonymous classes have names that are synthesized by the compiler, and these names may vary across compilers and may differ even from one build to the next, and class names must match for deserialization to succeed. Since the purpose of serialVersionUID is to help with serialization compatibility, including it in anonymous classes is pointless)

John Rose adds:

It only strengthen's Stuart's already unassailable conclusion, but I'll add:  ... may have a reference to an "enclosing instance" +[and/or references to captured local variable values] ... anonymous classes have names that are synthesized by the compiler ... +[Likewise, the classes may also have synthesized fields with unpredictably varying names and types] ...

 
 
Close
loading
Please Confirm
Close