• Post Reply Bookmark Topic Watch Topic
  • New Topic

Method Local Inner Class. problem accessing a Variable.  RSS feed

 
AmritP Singh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • 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
 
Chan Ag
Rancher
Posts: 1090
14
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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 --> http://www.coderanch.com/t/509890/java/java/accessing-shadowed-final-values-method

Chan.
 
AmritP Singh
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Chan for the update
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!