简体   繁体   中英

Method local inner class hides method fields

Given the following class definition

public class MethodLocalAccess {

    int m = 10;

    String show(){
        final int m = 20;
        final int n = 30;
        class MyClass{
            int m = 40;
            String someOtherMethod(){
                return "" + m + n + this.m + MyClass.this.m + MethodLocalAccess.this.m;
            }
        }

        MyClass object = new MyClass();
        return object.someOtherMethod();
    }

    public static void main(String[] args) {
        System.out.println(new MethodLocalAccess().show());
    }

}

Produces output 4030404010 , which is fairly established why. I want to know, if the local-variable final int m = 20; can be accessed inside the inner-class.

Other way around, fields declared in method-local inner-class having same name as that of method-local-variable, will permanently hide the latter.

What are you referring to is called variable shadowing ( link ).

If a declaration of a type (such as a member variable or a parameter name) in a particular scope (such as an inner class or a method definition) has the same name as another declaration in the enclosing scope, then the declaration shadows the declaration of the enclosing scope. You cannot refer to a shadowed declaration by its name alone.

Once you shadowed a variable, it is not accessible anymore without explicitly specifying its scope, if possible. The only solution in this case is to rename either outer or inner variable.

No you can't. The variable inside the function completely shadowed and you can't refer it anymore as Java doesn't have a way to refer function context.

However you can access top level variables with the context this even though they shadowed ( infact you are not shadowing and creating a local variable with same name ).

You can't, because it's shadowed.

You can't even with reflection, because reflection works on the type level, not on the byte-code.

You can, with additional tools, if you play with the generated byte-code.

In your case I don't think you can access the local variable m defined in the show method since you already have it declared in your inner class, therefore shadowing it.

Using constructs like ClassName.this.varName allows you to access only the members of that enclosing class. This means you can't use that type of expression to access shadowed local varibles defined in the method since they aren't members of that class.

  • Yes, the fields declared in method-local inner-class having same name as that of method-local-variable, will permanently hide the latter.

  • We can access the local-variable final int m = 20 inside the
    inner-class only if you don't have the instance variable of Myclass, int m = 40 is absent.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM