aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes method local inner class 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 "method local inner class" Watch "method local inner class" New topic
Author

method local inner class

Ashok Paulraj
Ranch Hand

Joined: Jul 07, 2003
Posts: 78
Hello All,
Whatz the real significance of Method Local inner class?
~ Shalini
Kathy Sierra
Cowgirl and Author
Ranch Hand

Joined: Oct 10, 2002
Posts: 1572
Howdy,
Remember that a method-local inner class means the class is defined INSIDE a method:

The code above has a little problem, though... look at 'x'! It is local to the method as well. What does this mean? That the variable 'x' lives on the stack. There is a chance that the object from class Bar (although in this code, we're not making any, so the inner class is completely useless since there you can't really construct an instance of that class outside that method!) can outlive the stack frame for this method. So while the variable 'x' would be blown away, the object of type Bar could still be around on the heap if it were passed to someone else. Now, that doesn't mean that they couldn't have crafted a way for the variable to have been made available to the inner object anyway, but they didn't. That's the rule for Java. You cannot, MUST not use local variables within a method local inner class.
So this code would not even compile, and would give you a message like:
Foo.java:11: local variable x is accessed from within inner class; needs to be declared final
int y = x + 24;
^
1 error
AHHHH... so what if the variable is final?
The compiler is perfectly happy with:

And now what about forward referencing??
Although we KNOW that an instance variable can be declared anywhere in a class, outside a method, even if the variable declaration falls BELOW a method that uses it:
class Boof {
void whatever() {
g = 3; // OK even though 'g' is declared later in the class...
}
int g;
}

But this IS a problem with a method-local inner class (just as it is with any other aspect of forward-referencing in a method).
So the following would NOT make the compiler happy:

This will give you something like:
Foo.java:11: cannot resolve symbol
symbol : variable x
location: class Bar
int y = x + 24;
In reality, method-local inner classes are often just for anonymous inner classes (in which the same rules apply... you can't make reference to a non-final local variable (which also includes non-final method parameters from the method in which the inner class is declared).
cheers,
Kathy
"We hates tricksy method-local inner classes"
-Gollum, right after his SCJP
"One exam to fool them all. One exam assigned them. One exam to bring them all and at Prometric grind them."
Whoo-hoo! Bert and I are off to see Return of the King tonight! (at the Really Big Almost-An-Imax Screen in Boulder)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: method local inner class