java - Access outer class function from Object of Inner Class -
i know there similar question put related issue, however, wasn't able resolve issue. i've tried simplify problem following code -
class outer { outer() {} class inner { inner() {} } void func() { system.out.println("outer"); } } public class nested { public static void main(string args[]) { outer oo = new outer(); outer.inner ii = oo.new inner(); // ii.func(); know won't work } }
can call outer class function "func()" object of inner class "ii"..?? if yes, how?
short answer: reference outer.this
private
in inner
cannot access reference outer
instance instance of inner
.
you can export reference thus:
class outer { outer() { } class inner { inner() { } public outer getouter() { return outer.this; } } void func() { system.out.println("outer"); } }
then can do:
ii.getouter().func();
Comments
Post a Comment