How do I access the method variable having the same name as the inner class member instance or method local variable of the inner class?
class A{
No. You can't do that. The reason being, the variable a
in position 2 is a local variable, which can only be accessed with it's simple name, in the enclosing scope. From JLS §6.4:
A local variable (§14.4), formal parameter (§8.4.1), exception parameter (§14.20), and local class (§14.3) can only be referred to using a simple name (§6.2), not a qualified name (§6.6).
Now, this clears that you can only access that variable with just a
. But, you have another variable in method local class B
which shadows that local variable a
in position 2, which is again shadowed by local variable in position 4.
Now in print statement, a
would access the variable from nearmost enclosing scope, that is local variable at position 4. If you remove that variable, it will print the variable at position 3. And then if you remove it, it will print the variable at position 2.
So, the point is, there is no way to access the local variable a
at position 2, because that is shadowed.