As far as I know, static fields and blocks are processed from top to bottom in a class. I mean, a field (static field) has to be declared before it can be used in static block. The static field must be declared first, then static block modifying that static field. Right?
Like this:
private static Map<String, Object> map = new HashMap<>();
static {
map.put("key", "value");
}
And it compiles. And if we reverse the ordering like this:
static {
map.put("key", "value");
}
private static Map<String, Object> map = new HashMap<>();
It fails to compile as expected.
However, if I write like this.
static {
map = new HashMap<>();
}
private static Map<String, Object> map;
It complies successfully?! Does anybody know what's going on? It doesn't mind if I assign a new object to map, but if I put something on the map it fails to compile.
Please give me a reasonable answer.