Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calling outer class function from inner class [duplicate]

Tags:

java

People also ask

Can inner class extend outer class?

Inner class can extend it's outer class. But, it does not serve any meaning. Because, even the private members of outer class are available inside the inner class. Even though, When an inner class extends its outer class, only fields and methods are inherited but not inner class itself.

Can inner classes access outer class members?

Non-static nested classes (inner classes) have access to other members of the outer/enclosing class, even if they are declared private.

Can inner class calling Outer method?

An instance of InnerClass can exist only within an instance of OuterClass and has direct access to the methods and fields of its enclosing instance. To instantiate an inner class, you must first instantiate the outer class.

How does an inner class instance access the outer class members?

If you want your inner class to access outer class instance variables then in the constructor for the inner class, include an argument that is a reference to the outer class instance. The outer class invokes the inner class constructor passing this as that argument.


You need to prefix the call by the outer class:

Outer.this.show();

This should do the trick:

Outer.Inner obj = new Outer().new Inner();
obj.show();