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.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…