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

Arjun Reddy
Ranch Hand

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

I was wondering if we can access Local variables of an Outer Class's method that has an Inner class. I wrote the following code and I don't know how to do it.I can access the member varible of outer class tho from the inner class tho.



Thanks.


Be Humble... Be Nice.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

If you make the local variables final, then you can access them from a method-local class.

For example, if i were final, then you could access it from InnerClass2.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Arjun Reddy
Ranch Hand

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

Thanks for ya reply. Can you tell me how I'd be able to do that from the code I wrote?

Thanks.
Madhukar Ojha
Ranch Hand

Joined: Mar 21, 2007
Posts: 71
Hi Rancher ,

In method local inner class , you can access only local final variables .
If you declare variable i as final , you can access it.


SCJP 5 ๑۩۞۩๑♥~~ My Life is My Creation ~~♥๑۩۞۩๑
Arjun Reddy
Ranch Hand

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

Can you tell me how to access the local variable tho after declaring it as final?

I was able to access the outer class instance variable using OuterClass.this.j but how do I access the local variable?

Thanks.
[ July 07, 2008: Message edited by: Arjun Reddy ]
Arjun Reddy
Ranch Hand

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

The post you closed was not a duplicate of this one. This post is regarding Method Local Inner Class and that was regarding Anonymous Inner Class. I did not want to post both of em in the same post as there might be some confusion.

Thanks.
Phal Ach
Ranch Hand

Joined: May 09, 2008
Posts: 54
Hi. Below is from geekevaluation.com, regarding method local inner class. I know that final variables of methods can only be accessed by its method local inner class. Then why not in below mentioned code?


Here a is final variable only. Still at Line 4, there is a compiler error. Somebody please give me a proper explanation for this. Thanks.
[ July 07, 2008: Message edited by: Phal Ach ]
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Originally posted by Arjun Reddy:
Hi Marc,

The post you closed was not a duplicate of this one. This post is regarding Method Local Inner Class and that was regarding Anonymous Inner Class. I did not want to post both of em in the same post as there might be some confusion.

Thanks.

Sorry. I re-opened the other one.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Method Local Inner class
 
Similar Threads
Access to "hidden" outer variables
static nested class
Confused with inner and nested classes
local class in a static method
Inner class Vs Outer Class