File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Mock Exam Errata and the fly likes ? inner class or local class member Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "? inner class or local class member" Watch "? inner class or local class member" New topic

? inner class or local class member

yun jiang

Joined: Mar 13, 2001
Posts: 3
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

Joined: Aug 26, 2000
Posts: 10065
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.

Uncontrolled vocabularies
"I try my best to make *all* my posts nice, even when I feel upset" -- Philippe Maquet
It is sorta covered in the JavaRanch Style Guide.
subject: ? inner class or local class member
It's not a secret anymore!