assuming the following example:
public abstract class A {
public void readList() {
for (Integer o : InnerA.INT_LIST) {
System.out.println(o);
}
}
public static class InnerA {
protected static List<Integer> INT_LIST;
static {
INT_LIST = new ArrayList<Integer>();
INT_LIST.add(1);
}
}
}
public class B extends A {
public static class InnerB extends InnerA {
static {
INT_LIST.add(2);
}
}
}
My assumtion was, that when I call
new B().readList();
the output would be
1
2
but instead it is
1
Adding the constructor
public B() {
new InnerB();
}
leads to the expected behaviour. I thought since the nested class is static, it gets initialized when a new B object is created (it obviously is when A is initialized).
Can someone please explain this to me?
Thanks.