If it isn’t defined as static then it implicitly keeps a reference to it’s outer instance. One issues is memory leaks due to entanglement.
JLS §8.6: Instance Initializers
This will create a special class for ‘bar’ and bar holds a reference to it’s container.
@SuppressWarnings("serial")
HashMap<String,String> bar = new HashMap<String, String>() {{
put("foo", "xyzzy");
put("bar", "yzzyx");
}};
which can be trivially reworked to this (without an anonymous class):
HashMap<String,String> bar = new HashMap<>();
{
bar.put("foo", "xyzzy");
bar.put("bar", "yzzyx");
}
There’s still a style gotcha here in that this is using an instance initializer and if ‘bar’ was changed to static the code would be executed each time a new instance of the container is created. The initializer would also have to be changed to a static one.