Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Mock exam Q- Which variables can an inner class access?

 
biju joseph
Greenhorn
Posts: 7
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Ron Newman
Ranch Hand
Posts: 1056
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This isn't a well-worded exam question. But an anonymous inner class is not implicitly static. And I don't think it's legal to declare a "local" static class (that is, inside a method).
 
Anthony Villanueva
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Biju,
If you are looking for more questions on inner classes, then try this one.

Would anyone like to suggest an answer?
 
bani kaali
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Dan Chisholm
Ranch Hand
Posts: 1865
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
bani,
Yes!
Your answer is correct.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic