• 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. problem accessing a Variable.

 
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi All,

I have a method local inner class. There are two variables with name "a", one is at the class level another one is at the method level.Both have the same name as "a". I want to print the values of "a" which is at the method level and NOT at the class level.

here is my code:-



Thanks for
 
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I think if it were an instance variable you wanted to access inside the method local class and if the method local class declared the same variable, you could use OuterClassName.this.variable to access the outer class variable. Not sure if you can access a local variable inside a method local class if the class hid it. Let's see what others suggest.

As a workaround, how about you pass the method local variable to the constructor of the method local class. But I guess you aren't looking for that kind of solutions?

Chan.
 
AmritP Singh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Chan,

Thanks for the reply. But "a" is not instance variable , its a Local variable for that method. Thanks for the workaround, but i am not looking for that solution. i have other workaround, but just wanted to learn shadowing inside the inner classes ..

AS
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

AmritP Singh wrote:Hi Chan,

Thanks for the reply. But "a" is not instance variable , its a Local variable for that method. Thanks for the workaround, but i am not looking for that solution. i have other workaround, but just wanted to learn shadowing inside the inner classes ..

AS



Accessing shadowed local variable from inside a method local inner class is NOT possible in Java through direct ( non workaround ) means. I just reconfirmed this.
You might want to check this --> https://coderanch.com/t/509890/java/java/accessing-shadowed-final-values-method

Chan.
 
AmritP Singh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Chan for the update
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic