File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes an urgent inner class question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "an urgent inner class question" Watch "an urgent inner class question" New topic

an urgent inner class question

Angie Wu

Joined: Aug 07, 2002
Posts: 10
class Outer{
int i;
final int j;
void containingMethod(int k, final int m) {
class Inner {
int n;
final int p;
void innerMethod() {
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()?
Kathy Sierra
Cowgirl and Author

Joined: Oct 10, 2002
Posts: 1589
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).

Co-Author of Head First Design Patterns
Just a Jini girl living in a J2EE world.
Angie Wu

Joined: Aug 07, 2002
Posts: 10
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.
Shishio San
Ranch Hand

Joined: Aug 29, 2002
Posts: 223
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.

Whatever doesn't kill us ...<br />Is probably circling back for another try.<br />SCJP 1.4
I agree. Here's the link:
subject: an urgent inner class question
It's not a secret anymore!