This week's book giveaway is in the Java in General forum.
We're giving away four copies of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 and have ishori Sharan & Adam L Davis on-line!
See this thread for details.
Win a copy of Beginning Java 17 Fundamentals: Object-Oriented Programming in Java 17 this week in the Java in General 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
  • Ron McLeod
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Jesse Silverman
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Frits Walraven

Wrong Answer?

 
Greenhorn
Posts: 23
  • 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.
1. public class Outer {
2. private static final int ID = 5;
3. private String name;
4. public void methodA(final int nn) {
5. int serialN = 22;
6. class inner {
7. void showResult() {
8. System.out.println("Rslt=" + XX);
9. }
10. } // end class inner
11. new inner().showResult();
12. } // end methodA
13. }
which variables would the statement in line 8 be able to use in place of XX? Check all which apply.
a. the int ID in line 2
b. the String name in line 3
c. the int nn in line 4
d. the int serialN in line 5
I think the answer should be A and C. But the answer mock exam showed is ABC. Is it a wrong answer?
Ada
 
Ranch Hand
Posts: 39
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
IMHO, the answer ABC is correct because inner classes in methods have access to
- all members of the class and outside the method
- final members only of the method that it is defined in.
Someone please confirm or correct this
Thanks
Sandeep
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Inner classes have access to only the final local variables of the outer class. "Final local variables" are the outer class method variables declared as final. Since serialN is not declared as final so the inner class can not access it.
 
mehrar
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by mehrar:
Inner classes have access to only the final local variables of the outer class. "Final local variables" are the outer class method variables declared as final. Since serialN is not declared as final so the inner class can not access it.


Here above the reference is in regard to the "outer class local variables". Otherwise inner class have access to all the variables of the outer class. I may sound incorrect above if "only" is interpreted for all outer calss variables.
 
Ada Wang
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Sandeep and Mehrar,
Thanks a lot for your replies. I got it!
Ada
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic