Does an outer class have access to private elements of its innerclass?

Discussion in 'Java' started by Dural, Feb 3, 2008.

  1. Dural

    Dural Guest

    My experimentation seems to say yes, but I can't seem to find it
    explicitly stated so anywhere.
    Dural, Feb 3, 2008
    #1
    1. Advertising

  2. Dural wrote:
    > My experimentation seems to say yes, but I can't seem to find it
    > explicitly stated so anywhere.


    Its stated in the JLS, 6.6.1 Determining Accessibility, "Otherwise, if
    the member or constructor is declared private, then 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."

    http://java.sun.com/docs/books/jls/third_edition/html/names.html#6.6.1

    Note that it says "body of the top level class", not the body of the
    class whose member it is.

    Patricia
    Patricia Shanahan, Feb 3, 2008
    #2
    1. Advertising

  3. Dural

    Dural Guest

    Here's the code I wrote which seems to indicate that I can access
    private fields of an inner class:

    public class Ex8 {

    class Ex8Inner {
    private int x;
    Ex8Inner(int x) {
    this.x = x;
    }
    }
    Ex8.Ex8Inner ei;
    Ex8() {
    ei = new Ex8Inner(10);
    }
    public static void main(String[] args) {
    Ex8 a = new Ex8();
    System.out.print(a.ei.x);
    }
    }
    Dural, Feb 3, 2008
    #3
  4. Dural

    Dural Guest

    Actually this works too, and is simpler:

    public class Ex8 {

    public class Ex8Inner {
    private int x = 5;
    }
    Ex8.Ex8Inner ei = new Ex8Inner();
    public static void main(String[] args) {
    Ex8 a = new Ex8();
    a.ei.x = 8;
    System.out.print(a.ei.x);
    }
    }
    Dural, Feb 3, 2008
    #4
  5. Dural wrote:
    > Actually this works too, and is simpler:
    >
    > public class Ex8 {
    >
    > public class Ex8Inner {
    > private int x = 5;
    > }
    > Ex8.Ex8Inner ei = new Ex8Inner();
    > public static void main(String[] args) {
    > Ex8 a = new Ex8();
    > a.ei.x = 8;
    > System.out.print(a.ei.x);
    > }
    > }


    Why is this an issue? The top level class enclosing the declaration of
    a.ei.x is Ex8. The accesses occur within the body of Ex8. They are
    permitted according to the quote from the JLS that I posted:

    "Otherwise, if the member or constructor is declared private, then
    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."

    Patricia
    Patricia Shanahan, Feb 3, 2008
    #5
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Heiko Henkelmann
    Replies:
    2
    Views:
    481
    Heiko Henkelmann
    Apr 27, 2004
  2. Vipin

    InnerClass Help

    Vipin, Mar 22, 2007, in forum: Java
    Replies:
    2
    Views:
    270
  3. thunk
    Replies:
    1
    Views:
    292
    thunk
    Mar 30, 2010
  4. thunk
    Replies:
    0
    Views:
    458
    thunk
    Apr 1, 2010
  5. Replies:
    2
    Views:
    226
Loading...

Share This Page