GeeCON Prague 2014*
The moose likes Java in General and the fly likes Method Local Inner Class Question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Java in General
Bookmark "Method Local Inner Class Question" Watch "Method Local Inner Class Question" New topic
Author

Method Local Inner Class Question

Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Hi,

I did not understand the following sentence from Kathy Sierra Book.

A method-local inner class cannot use variables declared within the method
(including parameters) unless those variables are marked final.


I wrote a sample code as below.


According to the above sentence from kb book, I made variable 'a' final but I cannot access it. I can only access the variable 'j' if it's final, but then again, I cannot access the method someMethod222() even if it's marked final?

Thanks.
[ September 19, 2008: Message edited by: Arjun Reddy ]

Be Humble... Be Nice.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
Maybe because you are calling "OuterClass.this" when the "a" is a local variable, not an instance field.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39044
    
  23
. . . and you can't usually use the keywords "this" and "super" in a static method because both keywords refer to instances.
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Hi Campbell,

Thanks for replying. Can you give me a code sample of what this means exactly?

A method-local inner class cannot use variables declared within the method
(including parameters) unless those variables are marked final.


Thanks.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18874
    
  40

Did you try....



Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Arjun Reddy
Ranch Hand

Joined: Nov 10, 2007
Posts: 627
Okie... it worked. Thanks Campbell and Henry
[ September 19, 2008: Message edited by: Arjun Reddy ]
 
GeeCON Prague 2014
 
subject: Method Local Inner Class Question