Is it possible to get a reference to this
from within a Java inner class?
i.e.
class Outer { void aMethod() { NewClass newClass = new NewClass() { void bMethod() { // How to I get access to "this" (pointing to outer) from here? } }; } }
Of your inner class is non static then create an object of innerClass. OuterClass out = new OuterClass(); OuterClass. InnerClass inn = out.
Yes. These qualifiers will only affect the visibility of the inner class in classes that derive from the outer class. Can inner class access members of outer class? Yes, including the ones declared private , just as any instance method can.
No, you cannot assume that the outer class holds an instance of the inner class; but that's irrelevant to the question. You're first question (whether the inner class holds an instance of the outer class) was the relevant question; but the answer is yes, always.
You can access the instance of the outer class like this:
Outer.this
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With