Search code examples
javainheritancestatic-methods

Static methods & inheritance – java.lang.IllegalAccessError not found at compile-time


I have file structure and code like this:

../inside/A.java

package inside;
public class A{protected static void someStaticMethod(){}}

../inside/B.java

package inside;
import inside.A;
public class B extends A{protected static void someStaticMethod(){}}

../inside/C.java

package inside;
import inside.B;
public class C extends B{protected static void someStaticMethod(){}}

../Z.java

import inside.*;
class Z extends B{
  public static void main(String args[]){
  A.someStaticMethod();
  B.someStaticMethod();
  C.someStaticMethod(); // Fine at compile-time but IllegalAccessError at run-time.
}

}

At line with comment there is no error at compile-time but at runtime there is IllegalAccesError.

What is the true reason for this behavior?

I have found a question – Why does Java bind variables at compile time? – where in the first answer there is maybe mentioned the reason but I am definitely not sure it is.


Solution

  • It is probably bug in javac – see http://www.coderanch.com/t/664583/java/java/Static-methods-inheritance-java-lang.