Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4117070

error accessing field of protected inner class inherited from different package

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Duplicate
    • Icon: P4 P4
    • None
    • 1.2.0
    • tools
    • None
    • sparc
    • solaris_2.5

      I've tried to browse through the unresolved bugs relating to inner classes
      and "protected" access, and I couldn't find one that seemed to describe the
      specific problem that I'm running into, so I'm filing it here; my apologies if
      this bug is actually a duplicate. Also, I'm guessing that this is a "compiler"
      bug and not a "runtime" bug because even though I am getting a failure at run
      time, my impression is that it is the compiler's responsibility to generate
      the necessary magic to make this sort of inner class access work.

      The runtime throws an IllegalAccessError when I try to access a (public) field
      in a protected nested class inherited from a superclass in a different package.
      Here is a simple example:

      // Super.java
      package a;
      public class Super {
          protected static class Foo {
              public Foo() {} // required because of 4087421??
              public int x;
          }
      }

      // Sub.java
      package b;
      public class Sub extends a.Super {
          public static void main(String[] args) {
              Foo f = new Foo();
              f.x = 2; // <== IllegalAccessError thrown here
          }
      }

      running "java b.Sub" with the JDK1.2Beta3-J promoted build, I get this error:

      java.lang.IllegalAccessError: try to access class a/Super$Foo from class b/Sub

      I want to do something like this, i.e. have a protected nested class with public
      fields, as a mechanism for returning multiple values from a protected method in
      the superclass; a container is all that I need, not data abstraction.

      Inspecting the bits of the class file a/Super$Foo.class, it is clear that it is
      generated as a package access (as opposed to public) top-level class, which
      certainly explains why the VM denies the access to the field from the code in
      another package. I have vague memories of an early Inner Classes Spec talking
      about compiling protected inner classes as *public* top-level classes to take
      care of this problem, but apparently that is not being done. Of course, this
      issue turns up more often now because of the VM subjecting *all* classes to
      access checks.

            wmaddoxsunw William Maddox (Inactive)
            peterjones Peter Jones (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: