-1

We know that objects of inner classes can only arise from objects of the enclosing class. So then why is it the case that an object of a inner class cannot invoke a non-static method of an enclosing class outside the definition of the enclosing class?

public class OuterClass
{       
public class InnerClass
    {
        public void innerMethodA()
        {
            outerMethodA(); // This is OK.
            new InnerClass().outerMethodA(); // This is not OK.
        }
    }

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

public class ExtraClass 
{
    public void testMethod()
    {
        OuterClass outerObj = new OuterClass();

        OuterClass.InnerClass innerObj = outerObj.new InnerClass();

        outerObj.outerMethodA();  // This is OK.
        innerObj.outerMethodA();  // This is not. Why is that?
    }
}

Please excuse any mistakes in formatting or the way I am putting forward my question. I edited it after some down votes. I tried my best. Thank you.

  • 1
    I think this needs code. I'm not really sure what you mean. – markspace May 21 '16 at 15:11
  • 2
    You need an instance of any class to call it's non-static methods. Inner classes don't magically have that instance just because it's enclosed – OneCricketeer May 21 '16 at 15:14
  • Why would you want to do something like this? What is the driving force behind this question? In what situation would this make sense? – Hovercraft Full Of Eels May 21 '16 at 15:42
  • I haven't tested this yet, but I *think* in both cases it is simply that `outerMethod` is not a member of `InnerClass`. – markspace May 21 '16 at 15:43
  • @Tom: It is not a duplicate. That question only talks about calling a an outer class method from within the inner class definition. – Bilal Mallick May 21 '16 at 15:43
  • `InnerClass` does not have a method `outerMethodA`. So you cannot call `outerMethodA` on an instance of `InnerClass`. – aventurin May 21 '16 at 15:47
  • This is what I am reading in Absolute Java by Savitch " to create an object of the inner class, you must start with object of the outer class. This has to be true, because an object of the inner class may invoke a method of the outer class or reference an instance variable of the outer class " – Bilal Mallick May 21 '16 at 16:02
  • Right-o. See my answer for an example how to do this. – markspace May 21 '16 at 16:03
  • This is in the 5th ed. Am I understanding something incorrectly. Why is it then that I cannot invoke a method of the outer class? – Bilal Mallick May 21 '16 at 16:03
  • "may" = "may chose to." May does not mean "can automatically do so without declaring any extra methods." – markspace May 21 '16 at 16:04
  • Also, there is a difference between the inner class itself, and all other classes such as `ExtraClass`. Notice I have to use the inner class to allow `ExtraClass` to access the outer method. – markspace May 21 '16 at 16:06

1 Answers1

1

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.
   }
}
Community
  • 1
  • 1
markspace
  • 10,621
  • 3
  • 25
  • 39