Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

enclosing class can access its inner class private members??

 
neel sri
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One of the study material for SCJP says:

"Enclosing class members can access the private variables and methods of its member classes. These private fields and methods can be accessed by other member classes also."

I tried with the following code:

ofcourse it did not work as the inner1var is restricted to the class inner. Which means that statement is wrong. can anyone throw some light on this.

Thanks,
Neel
Preparing for SCJP 1.4

[ February 06, 2005: Message edited by: neel sri ]
[ February 06, 2005: Message edited by: Jim Yingst ]
 
Jeroen Wenting
Ranch Hand
Posts: 5093
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
it's exactly the other way around...
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Member classes can access private member variables of other member classes (in the same top-level class) the same way they can access public members of other member classes. That is, the word private does not prevent the access, but there can still be other things which prevent it. In this case, since inner1var and inner2var are nonstatic member variables, you need to have an instance of inner1 or inner2 to access them. This has nothing to do with public or private - it's true for any instance variable. You need an instance of the class that declares it. Try this:
 
neel sri
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the ur replies ..
I was thinking the same ..
so can we conclude saying that the enclosing class cann't have direct access to the members of the inner classes?
 
Tony Morris
Ranch Hand
Posts: 1608
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic