Skip to Main Content

Java APIs

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

Uzivatel919Apr 18 2016 — edited Apr 18 2016

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.

Post Details
Locked on May 16 2016
Added on Apr 18 2016
0 comments
586 views