public abstract class Class1 {
protected static Object object1 = null;
protected static Object object2 = null;
public static Object[] objects = { object1, object2 };
public static void main(String[] args) {
new Class2();
for (Object o : objects) {
System.out.println(o);
}
}
}
public class Class2 extends Class1 {
public Class2() {
Class1.object1 = new String("String 1");
Class1.object2 = new String("String 2");
}
}
This outputs:
null
null
Why?
When I create a new instance of Class2
, the constructor for that class initializes object1
and object2
.
objects
, as far as I know, contains references to these Objects. So after they're initialized, I expected anything but null.
Could someone explain? Thanks.