Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

? inner class or local class member

 
yun jiang
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following code for a class in which methodA has an inner class, which variables would the statementin line 8 be able to use in place of XX? [Check all correct answers]
1. public class Base {
2. private static final int ID = 3;
3. private String name;
4. public void methodA(final int nn ) {
5. int serialN = 11;
6. class inner {
7. void showResult(){
8. System.out.println("Rslt= " + XX );
9. }
10. } // end class inner
11. new inner().showResult();
12. }
13. }

a. The int ID in line 2
b. The String in line 3
c. The int nn in line 4
d. The int serialN in line 5
The correct answers are a , b and c. I thought only c is correct.
If my concept is right, class inner is a local class member (not a inner class member) within the function block, therefore it can only access final variables in that enclosing function. Can anyone tell me why anwser a and b are also correct
 
Mapraputa Is
Leverager of our synergies
Sheriff
Posts: 10065
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
yun, if we define a variable inside a method, the scope for this variable is an enclosing method. But inner classes are another matter in that they are usually designed to help �main� class perform its task. They are useful for auxiliary tasks because they have access to all members (including private) of enclosing class. Class �inner� is a local inner class; and local inner classes, like member inner classes, have access to ALL variables of the enclosing class. The only restriction � they cannot access NON-FINAL variables of enclosing method.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic