Let's say I have a class
class A{
public final static TreeMap<String,String> tmap = new TreeMap<>();
int x;
static{
tmap.put("x:I", "Hello");
}
}
and I create a subclass
class B extends A{
long y;
static{
tmap.put("y:J","World");
}
}
If I now write some code to check the static initialisers:
class Main{
public static void main (String[] args){
B b = new B();
for(String v : b.tmap.values()){
System.out.println(v);
}
}
}
I know both entries must be in tmap
because A
must get loaded, eventually, for B
's super call at the very latest.
But if I'm reading When does static class initialization happen? correctly, I cannot assume that the Hello
value is put into the map first all the times because tmap
is final.
So if ordering were important (say if I knew there's a chance some values may be updated/overwritten further down the hierachy), do I need to remove the final
modifier?
Or is there something else already enforcing "proper" static initialiser ordering?