• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Method Local Inner class

 
Ranch Hand
Posts: 629
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 71
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
And then the flying monkeys attacked. My only defense was this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic