Hi, Following is a question from JohnJunt Mock Exam. Which variables can an inner class access from the class which encapsulates it? A. All static variables B. All final variables C. All instance variables D. Only final instance variables E. Only final static variables Select all correct answers
The answers are A, B,C. But i think this is wrong because: 1. methods of a static inner class cannot access instance variables or methods of the enclosing class. This is because static inner class doesnot have a reference to the enclosing class object. 2. methods of static local class cannot access instance variables of the enclosing class. same reason as above 3. if the anonymous inner class is static(implicitly), it cannot access any instance variables of the enclosing class When exam says inner class, which inner class do they mean?. Is that all inner classes or a specific type of inner class? Thanks biju
I agree that it's not a well-worded exam question. However, you can declare a "local" static class. Declare it inside a static method and don't use a static modifier for this class. It will be implicitly static. The same goes for an anonymous class.
this code will give complier error at line 8. static class StaticNested cannot access instance varible int c. Top level nested classes does not have access to non-static members of the enclosing class. -bani
posted 18 years ago
bani, Yes! Your answer is correct.
Dan Chisholm<br />SCJP 1.4<br /> <br /><a href="http://www.danchisholm.net/" target="_blank" rel="nofollow">Try my mock exam.</a>