3 Replies Latest reply: Apr 3, 2008 3:12 AM by 807591 RSS

    Inner class cannot access outer class method?

    807591
      consider this :

      class Outer
      {
      public void outerMethod(){
      }

      public void sameName(){
      }
      class Inner
      {
      public Inner(){
      outerMethod(); //calls outer class method
      sameName(); //calls inner method - but I want to call outer class method
      }

      public void sameName(){
      }
      }
      }


      How do I call the outer class method, which has the same name as an inner class method, from the inner class??
        • 1. Re: Inner class cannot access outer class method?
          807591
          class Outer
          {
          public void outerMethod(){}
          
          public void sameName(){
          }
          class Inner {
             Outer instance;
             public Inner(Outer instance){ this.instance = instance; }
          outerMethod(); //calls outer class method
          sameName() { instance.sameName(); } //calls inner method - but I want to call outer class method
          }
          
          public void sameName(){
          }
          }
          }
          • 2. Re: Inner class cannot access outer class method?
            807591
            //calls inner method - but I want to call outer class method
            If you have a question about runtime behaviour of your code, please take the time to ensure that your code does compile. (And formatting is nice too: put {code} at the start of your code and again at the end.)
            • 3. Re: Inner class cannot access outer class method?
              807591
              The following works for me:
              class Outer {
                  public void sameName() {
                      System.out.println(this);
                  }
                  
                  class Inner {
                      Outer instance;
                      public Inner(Outer instance){ 
                          this.instance = instance; 
                      }
                      public void sameName() {
                          System.out.println(this); // prints Outer$Inner@...
                          instance.sameName();      // prints Outer@...
                      } 
                  }
                  
                  public static void main(String args[]) {
                      Outer test = new Outer();
                       
                      Outer foo = new Outer();
                      Outer.Inner oi = foo.new Inner(test);
                       
                      System.out.println("test=" + test);
                      System.out.println("foo=" + foo);
                      System.out.println("oi=" + oi);
                       
                      oi.sameName();
                  }
              }
              The "instance.sameName()" line prints "Outer@..." showing that it it is, in fact calling the Outer class's sameName() method. The particular value printed shows that it is calling that method on test aka instance. And not tht of its enclosing object foo. For that I would need
              Outer.this.sameName();