Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Method Local Inner class

 
Arjun Reddy
Ranch Hand
Posts: 629
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arjun Reddy
Ranch Hand
Posts: 629
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 71
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Arjun Reddy
Ranch Hand
Posts: 629
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 629
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic