Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

? inner class or local class member

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Leverager of our synergies
Posts: 10065
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic