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

inner class access to private member (Java_rule: Round up#136)

 
Cindy Yang
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, there,
There is question(#136) in Java Rule Round-up game:
True or False:
An inner class has free access to private member data of the outer classes.
The answer is True. Why?
According to Khalid's "A programmer's Guide to Java Certification" (p239), if an inner class is located inside an static method(static local class), the inner class can only access static member of outer class.
Can anybody explain that for me?
Thanks a lot.

Cindy
 
Vedhas Pitkar
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
Yes a local inner class in a static method can access only static members of outer class but i think when u say an inner class ,i think it means a normal non static inner class.Right?
 
Carlisia Campos
sanitation engineer
Ranch Hand
Posts: 135
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's right: (non-static) inner classes can access directly all the instance variables and methods of the outer class object.
However, if an inner class is defined inside a mtehod of a class, it has access to its outer class's instance members and any (and only) final local varialbes in the method (including the argument parameters).

Now, static nested classes can only access static methods and static variables of the outer class.

--Carlisia
 
Cindy Yang
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you. I am clear now.
When I did that question, I thought the inner class in the question could be either static or non-static...
So, I need to pay attention to the trick.

Cindy
 
Anonymous
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!
Yes Carlisia (non-static) inner classes can access directly all the instance variables and methods of the outer class object but if non-static inner class is within a static menber, then non-static inner class can only access static methods and static variables of the outer class.

hope thats help
 
Jane Griscti
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,
Just an FYI ... a 'static' class, of any kind, is not an inner class. See JLS §8.1.2

An inner class is a nested class that is not explicitly or implicitly declared static.

------------------
Jane Griscti
Sun Certified Programmer for the Java� 2 Platform
 
vineeta bhatt
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
how can i access the instance members of outer class from a static inner class.??
 
Himai Minh
Ranch Hand
Posts: 1296
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
vineeta bhatt wrote:how can i access the instance members of outer class from a static inner class.??

One possible way may be:




 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic