fork download
  1. abstract class Super {
  2. protected static Object staticVar;
  3.  
  4. protected static void staticMethod() {
  5. System.out.println( staticVar );
  6. }
  7. }
  8.  
  9. class Sub extends Super {
  10. static {
  11. System.out.println("init");
  12. staticVar = new Object();
  13. }
  14.  
  15. /*
  16.   public static void someOtherMethod(){}
  17.   */
  18.  
  19. /* Implementing a method with the same name here avoids staticVar being null
  20.   public static void staticMethod() {
  21.   Super.staticMethod();
  22.   }*/
  23.  
  24. }
  25.  
  26. class UsersClass {
  27. public static void main( String[] args ) throws Exception {
  28. Class.forName("Sub");
  29. //Sub.someOtherMethod();
  30. new UsersClass().method();
  31. }
  32.  
  33. void method() {
  34. Sub.staticMethod(); // prints "null"
  35. }
  36. }
Success #stdin #stdout 0.09s 380224KB
stdin
Standard input is empty
stdout
init
java.lang.Object@8e2fb5