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?

Shivan Dragon
  • 15,004
  • 9
  • 62
  • 103
  • 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. – irreputable Oct 25 '11 at 19:39
  • relevant: [explicit serialVersionUID considered harmful?](http://stackoverflow.com/questions/419796/explicit-serialversionuid-considered-harmful) – Miserable Variable Oct 25 '11 at 19:42
  • @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 '11 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? – Shivan Dragon Oct 25 '11 at 19:49
  • @Andrei for transient serialization, the version is not useful anyway. Versioning is needed if serialization is persistent and survives multiple code bases - basically a poor man's database. Who does that today? As to Eclipse, sure it's a compiler, but most Eclipse users on SO talks about "compiler" without any qualification, as if it is **the** compiler. That is really not helpful to others who try to understand the question. Since this misuse is so prevalent, for whatever reason, there's no hope to correct it. But it reflects badly on the questioner. – irreputable Oct 25 '11 at 19:59
  • To everyone who's commenting, I agree with your observations about Serialization and the related field not being necessarily the best thing ever, but my question was why I get this warning. I think it's usually a good idea to disable warning that you know about so that the ones that you don't know (and thus the usefull ones) can be more visible. – Shivan Dragon Oct 25 '11 at 19:59
  • @Irreputable: again, I agree with you completly, Eclipse is not the compiler but an IDE, however (and I've re-read my question to make sure I didn't say stupid things) never in the body of my question do I state that Eclipse is the compiler, so again, why are you mentioning this here? – Shivan Dragon Oct 25 '11 at 20:01
  • 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 '11 at 20:04
  • When you present a compiler warning/error, you should always tell which compiler you are using. For some reason, Eclipse users on SO seldom do that. – irreputable Oct 25 '11 at 20:05
  • @Irreputable: no I shouldn't, I think that stating that I'm talking about a compile time error is enough in the context of this question. I could of course added the OS (with major.minor version), the hardware configuration etc etc, but that would just been useless bla for such a simple question. – Shivan Dragon Oct 25 '11 at 20:10
  • same question as in http://stackoverflow.com/questions/1926641/why-does-double-brace-initialization-asks-for-serialversionuid except that "anonymous" term is used instead of "double brace initialization" – gnat Oct 25 '11 at 20:49
  • @irreputable It is not acceptable to attempt to pass off your personal opinion as concrete fact. You don't have any evidence about who is and isnt' using Java Object Serialization The volume of questions about [tag:serialization] in the nearly ten years since you posted your unsubstantiated assertion clearly indicate your mistake. – user207421 Apr 21 '21 at 10:34

3 Answers3

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.

Jack Edmonds
  • 31,931
  • 18
  • 65
  • 77
  • 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. – Shivan Dragon Oct 25 '11 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`). – Jack Edmonds Oct 25 '11 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 '11 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.

G_H
  • 11,739
  • 3
  • 38
  • 82
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.

Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142