Clarification about accessibility of private members

Eddie Aftandilian eaftan at google.com
Fri Sep 30 21:31:11 UTC 2016


JLS 6.6.1 says, "Otherwise, the member or constructor is declared private,
and access is permitted if and only if it occurs within the body of the top
level class (ยง7.6) that encloses the declaration of the member or
constructor."  Under that definition, I believe the following code should
be legal:

class Holder {
  class Super {
    private String s1;
  }
  class Sub extends Super {
    private String doIt() {
      return s1;
    }
  }
}

Holder is the top level class that encloses the declaration of s1, and the
"return s1" statement occurs inside Holder's body.  However, javac 1.8.0
issues an error on this code:

Holder.java:7: error: s1 has private access in Holder.Super
      return s1;
             ^
1 error

What am I missing?

Thanks,
Eddie
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.openjdk.java.net/pipermail/compiler-dev/attachments/20160930/6e083a56/attachment.html>


More information about the compiler-dev mailing list