I have come across with this syntax while reading some others code
Map<String, String> map = new HashMap<String, String>() {
{
put("a", "b");
}
};
I know how to use anonymous inner class but this seems something different. Could somebody explain me how exactly the above works and how it is different from Map<String, String> map = new HashMap<String, String>(); map.put("a", "b"); ?
You are basically creating a anonymous class instance and specifying an instance initializer. Think of it in terms of a normal class, e.g.:
What do you think the above is doing? Your anonymous class is doing something similar. 🙂