Win a copy of Svelte and Sapper in Action this week in the JavaScript 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Wrong Answer?

 
Greenhorn
Posts: 23
  • 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.
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
  • 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
  • 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
  • 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
  • Quote
  • Report post to moderator
Hi Sandeep and Mehrar,
Thanks a lot for your replies. I got it!
Ada
 
So there I was, trapped in the jungle. And at the last minute, I was saved by this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic