1

I cannot access var variable from inner class method. but java is accessing to class variable instead of local variable. How can I access to local variable instead of class variable from inner class method ?

class Foo {
    void test() {
        int var = 3;

        class Inner {
            int var = 1;

            void print_var_of_test() {
                System.out.println(var); // this line prints 1 why ?
                // I want this line to print var of test (3) function.
            }
        }
    }
}
4
  • 1
    What happens if you give one of those variables a different name? Commented Oct 22, 2017 at 12:57
  • 1
    print_var_of_test(int var) Commented Oct 22, 2017 at 12:57
  • This isn't a case of a local variable and a class variable; it's an issue of two local variables. Commented Oct 22, 2017 at 13:21
  • @KevinAnderson, so you can edit my question to be better. Commented Oct 22, 2017 at 13:23

3 Answers 3

2

You cannot access a local variable defined in a method of the outer class from an inner class if the inner class defines already a variable with the same name.
You could use distinct names to distinguish them.

As workaround to keep the same variable names, you could change the signature of print_var_of_test() to accept an int.
In this way, outside the inner class you could pass the int var variable as the method is invoked.

class Foo {

  void test() {
    int var = 3;

    class Inner {
        int var = 1;

        void print_var_of_test(int var) {
          System.out.println("outer var=" + var);
          System.out.println("inner var=" + this.var);
        }
    }

    Inner inner = new Inner();
    inner.print_var_of_test(var);
  }

}
Sign up to request clarification or add additional context in comments.

3 Comments

is there a any other solution to solving this name problem ? because using different names is nonsense for me.
No you cannot. But as suggested by @mre you can use a workaround as changing the method to accept a parameter. I updated
Can you please justify why you cannot access? Last but not least this is not an inner class, but a local nested class.
1

You do that by picking another name for either the local variable or the inner class variable.

Since the inner class and the method variable are defined in the same source method, you should always be able to change one of them. There is never a situation in which you don't have the permission/right/capability to change one but not the other.

3 Comments

can I solve this problem without using different names ? using different names (this way) doesn't make sense for me.
@Nomad there isn't. The authors of the Java Language didn't think it was nonsense.
What kind of answer is this? I can also do not use local nested classes, then what? Of course I can rename it, but this does not provide an answer to the original post.
0

What about naming inner class var as varInner if you want to compare between it and the original one?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.