Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access outer class variable of same name?

I have made an Outer and an Inner class. Both these classes have variable int x. How to access x of Outer class in inner class. this.x is not working.

class OuterClass {
int x,y;
private class InnerClass {
    private void printSum(int x,int y) {
        this.x=x;
        this.y=y;
    }
  }
}
like image 917
Malwinder Singh Avatar asked Feb 10 '23 07:02

Malwinder Singh


1 Answers

You can try this :

   private void printSum(int x,int y) {
       OuterClass.this.x=x;
       OuterClass.this.y=y;
    }
like image 120
Sachin Gupta Avatar answered Feb 13 '23 05:02

Sachin Gupta