Hi,
this question is probably fairly common but I can't seem to find the answer around: Can somebody please explain the rationale behind the following behavior ?
public abstract class SuperClass {
static String mess;
}
public class SubClass extends SuperClass {
static {
mess = "Hello world!";
}
static String getMess() {
return mess;
}
}
public class mymain {
public static final void main(String[] args) {
System.out.println(SubClass.getMess());
}
}
gives "Hello world!" as expected whereas
public abstract class SuperClass {
static String mess;
static String getMess() {
return mess;
}
}
public class SubClass extends SuperClass {
static {
mess = "Hello world!";
}
}
public class mymain {
public static final void main(String[] args) {
System.out.println(SubClass.getMess());
}
}
gives "null". It looks like the initialization block is not executed. Why?
Thanks for your insight,
Chris