Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>A non-static inner class can access all methods of the enclosing class as if it were it's own methods:</p> <pre><code>public class Test { public int getOne() { return 1; } public class Inner { public int getEnclosingOne() { return getOne(); // this works... } } } </code></pre> <p>A static inner class can not, as a static inner class is not bound to an instance of the parent class. That can only call static methods on the enclosing class.</p> <p>As for methods when taking into account inheritance, an method in a non-static inner class can use all the methods of the enclosing (outer) class.</p> <p>The interesting part is <code>Test2.super.getOne()</code> which indeed obtains getOne() from <code>Test2.super</code>, which is a <code>Test</code>. This is just like Test2 would access the method, namely using <code>super</code> though prefixed with <code>Test2</code> to indicate you're accessing the namespace of the outer class. </p> <pre><code>public class Test2 extends Test { public int getInnerOuterParentOne() { Inner2 inner2 = new Inner2(); return inner2.getOuterParentOne(); } public int getInnerOuterOne() { Inner2 inner2 = new Inner2(); return inner2.getOuterOne(); } public int getOne() { return 2; } public class Inner2 { public int getOuterOne() { return getOne(); } public int getOuterParentOne() { return Test2.super.getOne(); } } public static void main(String[] args) { Test2 test2 = new Test2(); System.out.println(test2.getInnerOuterOne()); // 2 System.out.println(test2.getInnerOuterParentOne()); // 1 } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload