• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Junilu Lacar
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Knute Snortum
  • Tim Cooke
  • Devaka Cooray
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Ganesh Patekar

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

 
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
 
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?
 
sanitation engineer
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
 
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
 
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
 
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.??
 
Ranch Hand
Posts: 1751
12
  • 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:




 
when your children are suffering from your punishment, tell your them it will help them write good poetry when they are older. Like this tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!