Java User Group - Warnings clean-up hack day - 31st of Jan

Stuart Marks stuart.marks at oracle.com
Tue Jan 31 13:50:59 PST 2012


Ah, it looks like you guys wrapped up while I was taking my lunch break. I hope 
your hack session was fun and productive.

There was a question on IRC about the serialization warnings. There's some 
additional information I wanted to add, so I'll post it here for everyone's 
benefit.

--

The compiler will occasionally issue warnings of the form: "warning: [serial] 
serializable class Foo has no definition of serialVersionUID". Usually the fix 
is to add a static serialVersionUID field initialized to the proper value.

To get the proper serial version UID value, run the "serialver" tool using a 
*released* version of the JDK:

$ serialver java.misc.Foo
java.misc.Foo:    static final long serialVersionUID = 362498820763181265L;

Paste this line into Foo.java, with the addition of the "private" modifier:

     private static final long serialVersionUID = 362498820763181265L;

Note however that anonymous inner classes should *not* have a serialVersionUID. 
Instead, use @SuppressWarnings("serial") to suppress the serialization warning 
for this case. We probably need to fix the compiler so that it doesn't issue 
warnings for these cases.

(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.)

s'marks



More information about the jdk8-dev mailing list