Results 1 to 3 of 3
  1. #1
    lse123 is offline Senior Member
    Join Date
    Oct 2008
    Posts
    116
    Rep Power
    0

    Default how call from inner class(anonymous or not), a method of parent class?

    how call from inner class(anonymous or not), a method of parent class?

    class xxxXXX { // inner
    this.ParentMethod(); // this is correct?

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    As long as the method is not overloaded in the inner class, I believe that you can just call the method without requiring this. e.g.,

    Java Code:
    public void innerClassMethod() {
      parentClassMethod();  // no need for this.  
    }

    edit: yep. Test program:
    Java Code:
    public class OuterClass {
      public OuterClass() {
        InnerClass innerClass = new InnerClass();
        innerClass.innerClassMethod();
      }
      
      private void outerClassMethod() {
        System.out.println("outerClassMethod");
      }
      
      private class InnerClass {
        void innerClassMethod() {
          System.out.println("innerClassMethod");
          outerClassMethod();
        }
      }
      
      public static void main(String[] args) {
        new OuterClass();
      }
    
    }
    Last edited by Fubarable; 04-30-2010 at 11:32 PM.

  3. #3
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,251
    Rep Power
    19

    Default

    And if both classes have a method with the same signature, you can disambiguate by qualifying the method call.
    Java Code:
    public class Outer {
    
      public static void main(String[] args) {
        new Outer().new Inner().method();
      }
    
      void method() {
        System.out.println("Outer method");
      }
    
      private class Inner {
    
        void method() {
          System.out.println("Inner method");
          Outer.this.method();
        }
      }
    }
    db

Similar Threads

  1. How can I call method from class in other class??
    By Hisham in forum New To Java
    Replies: 6
    Last Post: 02-14-2010, 03:49 PM
  2. Child-Class Calling a Method in a Parent-Class
    By Blah_ in forum New To Java
    Replies: 5
    Last Post: 09-29-2009, 02:48 AM
  3. How to call a method from another class?
    By jboy in forum New To Java
    Replies: 8
    Last Post: 09-09-2009, 07:29 AM
  4. Replies: 7
    Last Post: 05-06-2009, 12:42 PM
  5. How to call a class within a method
    By Manfizy in forum New To Java
    Replies: 3
    Last Post: 03-19-2009, 12:34 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •