Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Does an outer class have access to private elements of its innerclass?

Reply
Thread Tools

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

 
 
Dural
Guest
Posts: n/a
 
      02-03-2008
My experimentation seems to say yes, but I can't seem to find it
explicitly stated so anywhere.
 
Reply With Quote
 
 
 
 
Patricia Shanahan
Guest
Posts: n/a
 
      02-03-2008
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/t...mes.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
 
Reply With Quote
 
 
 
 
Dural
Guest
Posts: n/a
 
      02-03-2008
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);
}
}
 
Reply With Quote
 
Dural
Guest
Posts: n/a
 
      02-03-2008
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);
}
}
 
Reply With Quote
 
Patricia Shanahan
Guest
Posts: n/a
 
      02-03-2008
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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Access to private data type of an outer class from inner class agentprog@gmail.com C++ 2 04-02-2013 04:48 PM
Public Data in Private Class or Private Data in Public Class? DaveLessnau C++ 3 05-16-2005 06:53 PM
inner class, explicit outer class method call Yamin Java 4 10-24-2004 06:17 AM
How to access outer class attribute from nested class Heiko Henkelmann Python 2 04-27-2004 07:37 PM
Access outer / inner class variables query lonelyplanet999 Java 3 11-18-2003 07:16 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57