wood burning stoves*
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
Author

an urgent inner class question

Angie Wu
Greenhorn

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() {
//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()?
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
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


Co-Author of <a href="http://www.amazon.com/exec/obidos/ASIN/0596007124/ref=jranch-20" target="_blank" rel="nofollow">"Head First Design Patterns"</a><br /> <br />Just a Jini girl living in a J2EE world.
Angie Wu
Greenhorn

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: http://aspose.com/file-tools
 
subject: an urgent inner class question
 
Similar Threads
Fybunic series
Nested vs Inner
what is the reason?
final variable and local variable
Confusion about Inner Class Classifications