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.
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 Campos<br />--------------------------------<br />i blog here: carlisia.com
Joined: Aug 24, 2001
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.
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.