3
public class MyClass {
    int x=9;
    public static void main(String args[]) {
       MyClass  myClass = new MyClass();
       myClass.test();
    }

    public void test(){
        int x=10;

        class InnerClass{
            int x = 11;

            void print(){
               int x = 12;
               System.out.println(x); 
               System.out.println(this.x); 
               System.out.println(MyClass.this.x); 
               System.out.println("MyClass => test() => x :" + "?");
            }
        }
        InnerClass innerClass = new InnerClass();
        innerClass.print();
    }
}

How to call MyClass test() method local variable x inside the InnerClass print() method. What i can write in place of ? in last System.out.println() method in order to get the value of test() x.

Dumbo
  • 1,630
  • 18
  • 33
  • 2
    Just because you call a datatype 'InnerClass', doesn't make it an 'inner class', this is just an instance variable, not an inner class. – Stultuske Feb 15 '18 at 07:07
  • just a thought: what is stopping you from passing that 10 value as parameter to the print method? – Stultuske Feb 15 '18 at 07:22
  • @Stultuske the class named `InnerClass` is not the instance variable, it is method-local inner class, because it's the class in the outer class method. – Dumbo Feb 15 '18 at 07:23
  • Duplication of https://stackoverflow.com/questions/48801326/how-can-i-access-this-m1-method-in-the-following-program/48801624#48801624 – Oleg Cherednik Feb 15 '18 at 07:27
  • 1
    @EJusius nope. that makes it a 'local class', not an 'inner class'. https://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html – Stultuske Feb 15 '18 at 07:27
  • @Stultuske, ok but i just want to know the scenario. – Ankit Kumar Feb 15 '18 at 07:30
  • @AnkitKumar as I have asked before: what stops you from passing the correct value as parameter? – Stultuske Feb 15 '18 at 07:32

3 Answers3

1

Unfortunately in Java you can't.

The only way to access the x in MyClass::test would be to rename both variables in your inner class and in your inner class method into something else.

There is no need though to rename the outer class field x as InnerClass::print would consider the variable in the most-inner scope.

Although this snippet is for demonstration purposes, better practice would have you have different and more significant names for each variable.

payloc91
  • 3,724
  • 1
  • 17
  • 45
-2

That works fine for me:

public class Outerclass {

    public static void main(String args[]) {
        Outerclass outer = new Outerclass();
        outer.outerMethod();
    }

    // method of the outer class 
    void outerMethod() {
        int num = 23;

        // method-local inner class
        class MethodInnerClass {
            public void print() {
                System.out.println("This is method inner class "+num);
            }
        }
        // Accessing the inner class
        MethodInnerClass inner = new MethodInnerClass();
        inner.print();
    }
}
Dumbo
  • 1,630
  • 18
  • 33
  • no doubt it does, but this doesn't reflect the code of the OP. in the code of the OP, there is also an instance variable with the same name. – Stultuske Feb 15 '18 at 07:32
-3

To print outer class variable use

 MyClass.this.x
Vishal Kawade
  • 449
  • 6
  • 20
  • Mr. Ankit Kumar asking how to access `test()` method variable `x`. Pay attention when you read the question :) – Dumbo Feb 15 '18 at 07:16
  • By using "MyClass.this.x" we will get the value of MyClass variable x. means this will return us 9. but i want to print x = 10. – Ankit Kumar Feb 15 '18 at 07:17
  • @AnkitKumar that 10 is a local variable inside a method, so you are not trying to reach a variable inside the class. please learn about scopes before trying to do something like this. – Stultuske Feb 15 '18 at 07:21
  • @Stultuske ok, but i change the variable x to y then i am able to access the y inside the inner class method, means the variable is in scope. – Ankit Kumar Feb 15 '18 at 07:27
  • 1
    @AnkitKumar if both an instance variable and a local variable have the same name, the compiler won't know what you mean, and take what it ('s programmed to) assume what you mean. – Stultuske Feb 15 '18 at 07:29