Forum Stats

  • 3,876,051 Users
  • 2,267,037 Discussions
  • 7,912,410 Comments

Discussions

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

Uzivatel919
Uzivatel919 Member Posts: 19

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.

This discussion has been closed.