Why is calling a method (non-static) of the outer class with an object of an inner class not allowed outside the definition of the outer class?


ANSWERS:


Per this stack overflow question, it isn't possible (in practice) to get an enclosing class from an inner class.

get the enclosing class object from anonymous inner class as function parameter

The only solution I see is to provide a method on InnerClass which makes the call for you.

class OuterClass
{

   public class InnerClass
   {
      public void innerMethodA()
      {
         outerMethodA(); // This is OK.
         enclosingInstanceMethodA(); // This is OK now.
      }

      public void enclosingInstanceMethodA()
      {
         OuterClass.this.outerMethodA();
      }
   }

   public void outerMethodA()
   {
      System.out.println( "This is OuterMethodA" );
   }
}

class ExtraClass
{

   public void testMethod()
   {
      OuterClass outerObj = new OuterClass();
      OuterClass.InnerClass innerObj = outerObj.new InnerClass();
      outerObj.outerMethodA();  // This is OK.
      innerObj.enclosingInstanceMethodA();  // This is OK now.
   }
}


 MORE:


 ? an object reference error messages is required for the non static field or property method
 ? Error to use of static and static function in C++
 ? non-static method cannot be referenced confusion
 ? Getting Object reference required for the non-static method
 ? how to access static and non static method in static method
 ? UWP how to change ListView binding Observable Colletion outside the page class
 ? Calling a non-static method in a static class - java
 ? error: non-static method PostFixConverter(String) cannot be referenced from a static context
 ? Process.Kill non-static field error
 ? MouseListener and JPanel Objects