5

I was wondering why when I use an anonymous instanciation along with an instance initializer block, I get a "serializable class does not declare a static final serialVersionUID field of type long" compile-time warning.

Here's what I mean. Let's say I want to instantiate an ArrayList and at the same time add something to it like so:

ArrayList<Object> arrayList = new ArrayList<Object>(){{add(new Object());}}; 

If I compile this all is ok but I get the serialVersionUID field missing warning. Now ArrayList already implements serializable and has a private static final long serialVersionUID so why is it that when I use it like that it seems that that field "dissapears" and I get a warning for not having it declared?

12
  • 1
    This is just silly. Few uses Java serialization today, such generic warning is unwarranted. And I can never get over Eclipse users referring to the IDE as the unqualified compiler. Oct 25, 2011 at 19:39
  • @irreputable Isn't there some annotation to suppress this? I mean, if they can do that for unchecked casts, surely you'd expect something like the UID to be ignorable.
    – G_H
    Oct 25, 2011 at 19:45
  • 1
    @irreputable: Well, the first part of your statement I don't agree with. Serialization has it's well delimited uses. Besides, what if your instance is in the HttpRequest object in an app server, and that app server decides to passivate and thus serialize it? And the part about Eclipse I don't understand. Eclipse can compile your Java code using the compiler, and you can just as well compile it by hand in the console (using -Xlint in this case for the warnings, which I always do). Can you please clarify? Oct 25, 2011 at 19:49
  • 2
    @irreputable - just because you have little need for serialization and/or its advanced features doesn't mean the rest of the world shares your feelings. e.g. versioning is useful for building a robust distributed system where disparate components may not be upgraded all at the same time.
    – jtahlborn
    Oct 25, 2011 at 20:04

3 Answers 3

8

When you create your anonymous class, you are actually extending ArrayList and therefore, inheriting the Serializable interface.

All Serializable classes are supposed to have a serialVersionUID so that you can distinguish between different serialized versions of the classes. Since the anonymous type is a new class, it would be a good idea to give it an ID so you can distinguish between different versions of it.

3
  • 3
    Got it, so this is just like extending the ArrayList class and then THAT extending class has the initializer block with the call to "add" but it's a different class, it needs it's own serialVersionUID (since it inherits Serializable from ArrayList). That wasn't obvious to me due to the fact that I was creating an anonymous class. Oct 25, 2011 at 19:39
  • 1
    @AndreiBodnarescu Exactly. Anonymous classes are just syntactic sugar for creating a new class that extends from another class (in your case, ArrayList). Oct 25, 2011 at 19:42
  • 2
    @AndreiBodnarescu Precisely. That said and done, using this double braces syntax for collection priming is discouraged since now you don't really have an ArrayList but rather some vague subclass, which might cause obscure problems later on. Probably not too much chance you run into those problems, but when you do it'll be a headscratcher. It may be verbose, but you're better off just writing all the add() statements or initializing an array and then creating the list using that.
    – G_H
    Oct 25, 2011 at 19:44
3

Because you're creating what's essentially a subclass. Such a subclass needs its own serial version UID. Same thing happens when you subclass things like JPanel. It's not a terrible problem if you don't require (de)serialization.

2
new ArrayList<Object>() {

    {
        add(new Object());
    }

};

You are not just instantiating but first defining a subclass (anonymous) of ArrayList and then instantiating the subclass.

Even though there is a private static final long serialVersionUID in ArrayList, since it's static, its not inherited by your anonymous subclass. So it's missing that field.

Not the answer you're looking for? Browse other questions tagged or ask your own question.