I know that this code:
Set<String> set = new HashSet<String>() {{
add("test1");
add("test2");
}};
is really:
Set<String> set = new HashSet<String>() {
{//initializer
add("test1");
add("test2");
}
};
The initializer block is being executed before the constructor block. In the above example, add("test1") is called before the constructor being executed. The constructor may be initializing many of the instance fields, so that this class would work. I am wondering why calling .add() before constructor would work? Is there any case that cause an issue?