abstract class Super {
protected static Object staticVar
;
protected static void staticMethod() {
System.
out.
println( staticVar
); }
}
class Sub extends Super {
static {
}
/*
public static void someOtherMethod(){}
*/
/* Implementing a method with the same name here avoids staticVar being null
public static void staticMethod() {
Super.staticMethod();
}*/
}
class UsersClass {
Class.forName("Sub");
//Sub.someOtherMethod();
new UsersClass().method();
}
void method() {
Sub.staticMethod(); // prints "null"
}
}
YWJzdHJhY3QgY2xhc3MgU3VwZXIgewogICAgcHJvdGVjdGVkIHN0YXRpYyBPYmplY3Qgc3RhdGljVmFyOwoKICAgIHByb3RlY3RlZCBzdGF0aWMgdm9pZCBzdGF0aWNNZXRob2QoKSB7CiAgICAgICAgU3lzdGVtLm91dC5wcmludGxuKCBzdGF0aWNWYXIgKTsKICAgIH0KfQoKY2xhc3MgU3ViIGV4dGVuZHMgU3VwZXIgewogICAgc3RhdGljIHsKICAgIAlTeXN0ZW0ub3V0LnByaW50bG4oImluaXQiKTsKICAgICAgICBzdGF0aWNWYXIgPSBuZXcgT2JqZWN0KCk7CiAgICB9CgogICAvKgogICBwdWJsaWMgc3RhdGljIHZvaWQgc29tZU90aGVyTWV0aG9kKCl7fQogICAqLwoKICAgIC8qIEltcGxlbWVudGluZyBhIG1ldGhvZCB3aXRoIHRoZSBzYW1lIG5hbWUgaGVyZSBhdm9pZHMgc3RhdGljVmFyIGJlaW5nIG51bGwKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBzdGF0aWNNZXRob2QoKSB7CiAgICAgICAgU3VwZXIuc3RhdGljTWV0aG9kKCk7CiAgICB9Ki8KICAgIAp9CgpjbGFzcyBVc2Vyc0NsYXNzIHsKICAgIHB1YmxpYyBzdGF0aWMgdm9pZCBtYWluKCBTdHJpbmdbXSBhcmdzICkgdGhyb3dzIEV4Y2VwdGlvbiB7CiAgICAgICAgQ2xhc3MuZm9yTmFtZSgiU3ViIik7CiAgICAgICAgLy9TdWIuc29tZU90aGVyTWV0aG9kKCk7CiAgICAgICAgbmV3IFVzZXJzQ2xhc3MoKS5tZXRob2QoKTsKICAgIH0KCiAgICB2b2lkIG1ldGhvZCgpIHsKICAgICAgICBTdWIuc3RhdGljTWV0aG9kKCk7IC8vIHByaW50cyAibnVsbCIKICAgIH0KfQ==