Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i access OuterClass through inner class instance? [duplicate]

Tags:

java

class

nested

I know i can get the instance of the enclosing class using:

OuterClass.this

But i can do it only within the inner class code itself. only I have is reference of the Inner class instance withing another class code. How can i get the OuterClass instance through the inner class instance?

like image 690
Shelef Avatar asked Apr 20 '26 12:04

Shelef


1 Answers

You can define a getter that returns OuterClass to callers from the outside:

public class InnerClass {
    ...
    public OuterClass getOuterInstance() {
        return OuterClass.this;
    }
}
like image 126
Sergey Kalinichenko Avatar answered Apr 23 '26 02:04

Sergey Kalinichenko



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!