wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Private Access Q Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Private Access Q" Watch "Private Access Q" New topic
Author

Private Access Q

Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
Following is an example of outer - inner class.



It gives output of "Roundhay".


My Question: Two classes are created, TropicalWorld (Outer) and TropicalWorld$Seacroft (Inner). Now, access of method getName is private to class Seacroft. I am confused, how another class (TropicalWorld) can access private member of Seacroft ?


Following is statement from Sun Java Tutorial for private access : "access restrictions apply at the class or type level (all instances of a class)"

(formatting improved)
[ November 26, 2004: Message edited by: Barry Gaunt ]

"Imagination is more important than knowledge. Knowledge is limited. Imagination encircles the world."
sanjeevmehra mehra
Ranch Hand

Joined: Aug 21, 2004
Posts: 75
Non static inner classes are defined as instance members of other classes, just like instance variable and method members are defined in a class. If that's the case then
[ November 23, 2004: Message edited by: sanjeevmehra mehra ]

thanks & regards,<br />Sanjeev.
Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
Thank you sanjeev, still my question reamins same.. If member of a class is PRIVATE, how other class can access that? Any detail explanation will be appreciated.
sanjeevmehra mehra
Ranch Hand

Joined: Aug 21, 2004
Posts: 75
even I am waiting for an answer.
[ November 23, 2004: Message edited by: sanjeevmehra mehra ]
Federico Zuppa
Greenhorn

Joined: Nov 23, 2004
Posts: 7
Could it be that the declaration of this private variable is considered to be inside the class body of the Outer class declaration ?

in 8.1.5 it says "The body of a class declares members (fields and methods and nested classes and interfaces), instance and static initializers, and constructors (�8.1.5). The scope (�6.3) of a member (�8.2) is the entire declaration of the class to which the member belongs.

and this is the grammar:
ClassBody:
{ ClassBodyDeclarationsopt }

ClassBodyDeclarations:
ClassBodyDeclaration
ClassBodyDeclarations ClassBodyDeclaration

ClassBodyDeclaration:
ClassMemberDeclaration
InstanceInitializer
StaticInitializer
ConstructorDeclaration

ClassMemberDeclaration:
FieldDeclaration
MethodDeclaration
ClassDeclaration
InterfaceDeclaration
;

So everything declared inside ClassBody (-MethodDeclaration-) is in the same scope ? (confused...)
prajkta patil
Ranch Hand

Joined: Nov 13, 2004
Posts: 49
public class TropicalWorld {

public static void main(String[]args){
TropicalWorld l = new TropicalWorld();
}

TropicalWorld(){
Seacroft s = new Seacroft();
System.out.println(s.getName());
System.out.println(s.i);
}

private class Seacroft{
private String getName(){
return "Roundhay";}
private int i=3;
}
}

it gives output:
Roundhay
3

even variable i is private.....
why?
Inuka Vincit
Ranch Hand

Joined: Aug 10, 2004
Posts: 175
http://java.sun.com/docs/books/jls/second_edition/html/statements.doc.html#247766


The scope of a local class declared in a block is the rest of the immediately enclosing block, including its own class declaration.


MCP (C# application dev 70-316) 860<br />SCJP 1.4 100% SCJD (URLyBird) 378<br />MAD 100% nuts
Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
Thank you for a nice link, but sorry, still confused!
Inuka Vincit
Ranch Hand

Joined: Aug 10, 2004
Posts: 175
In inner classes the member(variable and method) scope is within the definition of the whole class(outer class). Therefore the inner class has full access rights to the outer class, and the outer class has full access rights to the inner class. Ofcourse normal static/instance variable accessiblity rules apply.

Here is an example that proves what I am talking about. Run it play around with it, it will make things clear.


[B] [/B]
[ November 25, 2004: Message edited by: Inuka Vincit ]
JayaSiji Gopal
Ranch Hand

Joined: Sep 27, 2004
Posts: 303
Hi!

In the example, we have a top level class, within which we have an inner class. The top level class encloses the inner class. Hence, it has access to all the variables of the inner class, including private.

Regards,
Jayashree.
Nitin Bhagwat
Ranch Hand

Joined: Sep 09, 2004
Posts: 132
Thank you very much for your efforts Inuka Vincit. Played with it and understood it now.
 
 
subject: Private Access Q