• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

an urgent inner class question

 
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Outer{
int i;
final int j;
void containingMethod(int k, final int m) {
class Inner {
int n;
final int p;
void innerMethod() {
//whatever
}
}
}
}
what variable(s) can be read in the method innerMethod()?
The answer is i, j,m,n,p, why? Should only the final variable be read in the method innerMethod()?
 
Cowgirl and Author
Posts: 1589
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
First off, the code won't compile because the final variables p and j haven't been initialized. So if one of the answers is, "Code will not compile", that would be the one. But assuming those are simply big mistakes, and that you are to assume they've been taken care of..
the method-local inner class (Inner) has access to i and j, because they're instance variables of the outer class. And it also has access to m because its final. In fact, the only thing it does NOT have access to is 'k'.
The inner class can see "n" and "p" (the final on p makes no difference here) because both n and p are instance variables of the inner class, NOT local variables within the method itself. In other words, if you moved the declaration of n to become the first statement in the method (so, above/outside the inner class declaration) then indeed, n would not be accessible because its not final (and thus would be blown off the stack potentially long before the inner object was killed).
cheers,
Kathy
 
Angie Wu
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for your explanations.
Also, thanks for pointing out the initialization mistake in the question. In fact, this question is from the "100 Bonus Question" part in the "Complete Java 2 Certification study guide" book(3rd Edition). People who are studying this book for the exam should also be aware of this error.
 
Ranch Hand
Posts: 223
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Angie Wu:

Should only the final variable be read in the method innerMethod()?


Hi, The following explains why only final variables can be access by local class.


Local classes
�Can access all the features of the enclosing class (because they are defined inside the method of the class) but can access only final variables defined inside the method (including method arguments). This is because the class can outlive the method, but the method local variables will go out of scope � in case of final variables, compiler makes a copy of those variables to be used by the class. (New meaning for final)
�Since the names of local classes are not visible outside the local context, references of these classes cannot be declared outside. So their functionality could be accessed only via super-class references (either interfaces or classes). Objects of those class types are created inside methods and returned as super-class type references to the outside world. This is the reason that they can only access final variables within the local block. That way, the value of the variable can be always made available to the objects returned from the local context to outside world.

 
Tongue wrestling. It's not what you think. And here, take this tiny ad. You'll need it.
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic