I originally thought that static blocks were for static variables but the compiler allows both A and B to compile and run, what gives?
A
private static final Map<String,String> m = new HashMap<String,String>();
{
m.put("why", "does");
m.put("this","work");
}
B
private static final Map<String,String> m = new HashMap<String,String>();
static{
m.put("why", "does");
m.put("this","work");
}
Running System.out.println(Main.m.toString());
for A prints
{}
but running the same for B prints out in Yoda-speak
{this=work, why=does}