aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes inner class access to private member (Java_rule: Round up#136) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "inner class access to private member (Java_rule: Round up#136)" Watch "inner class access to private member (Java_rule: Round up#136)" New topic
Author

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

Cindy Yang
Greenhorn

Joined: Aug 24, 2001
Posts: 7
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

Joined: Jan 27, 2001
Posts: 445
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

Joined: Aug 22, 2001
Posts: 135
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


i blog here: carlisia.com
Cindy Yang
Greenhorn

Joined: Aug 24, 2001
Posts: 7
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

Joined: Nov 22, 2008
Posts: 18944
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

Joined: Aug 30, 2000
Posts: 3141
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


Jane Griscti
SCJP, Co-author Mike Meyers' Java 2 Certification Passport
vineeta bhatt
Greenhorn

Joined: Jan 05, 2013
Posts: 1
how can i access the instance members of outer class from a static inner class.??
Himai Minh
Ranch Hand

Joined: Jul 29, 2012
Posts: 796
    
    1
vineeta bhatt wrote:how can i access the instance members of outer class from a static inner class.??

One possible way may be:




 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: inner class access to private member (Java_rule: Round up#136)