• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 445
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
sanitation engineer
Posts: 135
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 18944
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 3141
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how can i access the instance members of outer class from a static inner class.??
 
Bartender
Posts: 2418
13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:




 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic