Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access shadowed Outer class variable from Inner class?

Tags:

java

scope

This is not straight forward question. In my case the outer class variable and the inner class setter method's argument name is same. like:

class Problem {
    String s;
    int p;
    class Inner {
        String testMethod() {
         return  s = "Set from Inner";
        }
        void setP(int p)
        {
            p=p;  //it will do self assignment
        }
    }


}

now I cant initialize outer class instance variable p with this.p=p as it indicates the inner class. again I cannot do Problem.p=p; it gets an error. Now how can I assign outer p, keeping the inner Class method setP(int p)'s argument the same name p ?

like image 334
Md. Arafat Al Mahmud Avatar asked Sep 10 '12 06:09

Md. Arafat Al Mahmud


2 Answers

This is how you can/should do it:

Problem.this.p
like image 134
yegor256 Avatar answered Oct 14 '22 23:10

yegor256


Use to refere p to Outer class like

Problem.this.p = p;
like image 27
Nandkumar Tekale Avatar answered Oct 14 '22 23:10

Nandkumar Tekale