I have some static initializer code:
someMethodThatTakesAHashMap(new HashMap<K, V>() { { put("a","value-a"); put("c","value-c");} });
For some reason I am receiving a warning from Eclipse: The serializable class does not declare a static final serialVersionUID.
Is this complaining about the anonymous class? What can I do about that, or should I just suppress it.
If serialVersionUID is not provided in a Serializable class, the JVM will generate one automatically. However, it is good practice to provide the serialVersionUID value and update it after changes to the class so that we can have control over the serialization/deserialization process.
However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values, since the default serialVersionUID computation is highly sensitive to class details that may vary depending on compiler implementations, and can thus result in unexpected InvalidClassExceptions during ...
SerialVersionUID is a unique identifier for each class, JVM uses it to compare the versions of the class ensuring that the same class was used during Serialization is loaded during Deserialization. Specifying one gives more control, though JVM does generate one if you don't specify.
The SerialVersionUID can be used during deserialization to verify that the sender and receiver of a serialized object have loaded classes for that object that are compatible w.r.t serialization. If the deserialization object is different than serialization, then it can throw an InvalidClassException.
The syntax you're using is called double-brace initialization - which is actually an "instance initialization block that is part of an anonymous inner class" (certainly not a hack). So, when using this notation, you are actually defining a new class(!).
The "problem" in your case is that HashMap
implements Serializable
. This interface doesn't have any methods and serves only to identify the semantics of being serializable. In other words, it's a marker interface and you concretely don't have to implement anything. But, during deserialization, Java uses a version number called a serialVersionUID
to verify that the serialized version is compatible with the target. If you don't provide this serialVersionUID
, it will be calculated. And, as documented in the javadoc of Serializable
, the calculated value is extremely sensitive and it is thus recommended be explicitly declare it to avoid any deserialization problems. And this is what Eclipse is "complaining" about (note that this is just a warning).
So, to avoid this warning, you could add a serialVersionUID
to your annonymous inner class:
someMethodThatTakesAHashMap(new HashMap<String, String>() { private static final long serialVersionUID = -1113582265865921787L; { put("a", "value-a"); put("c", "value-c"); } });
But you loose the conciseness of the syntax (and you may not even need it).
Another option would thus be to ignore the warning by adding a @SuppressWarnings("serial")
to the method where you are calling someMethodThatTakesAHashMap(Map)
. This seems more appropriate in your case.
That all being said, while this syntax is concise, it has some drawbacks. First, if you hold a reference on the object initialized using a double-brace initialization, you implicitly hold a reference to the outer object which won't be eligible for garbage collection. So be careful. Second (this sounds like micro optimization though), double-brace initialization has a very a little bit of overhead. Third, this technique actually uses anonymous inner classes as we saw and thus eats a bit of permgen space (but I doubt that this is really a problem unless you really abuse them). Finally - and this is maybe the most important point - I am not sure it makes the code more readable (it's not a well known syntax).
So, while I like to use it in tests (for the conciseness), I tend to avoid using it in "regular" code.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With