Call A Method From Within Inner Class

Discussion in 'Java' started by Martijn Mulder, Jul 1, 2005.

  1. From within an inner class I call a method in the enclosing
    class with the same name but WITH A DIFFERENT SIGNATURE. The
    compiler (jikes) complains:



    Found 1 semantic error compiling "./Mouse.java":

    25. mouseMoved(a.getX(),a.getY());
    <--------------------------->
    *** Error: The method "void mouseMoved(int a, int b);" contained in
    the enclosing type "Mouse" is a perfect match for this method call.
    However, it is not visible in this nested class because a method
    with the same name in an intervening class is hiding it.**


    Why is this? The name is the same but the signature is not.
    A slight change in the name (houseMoved instead of mouseMoved)
    makes it all work. What is the 'intervening class' jikes mouns
    about?




    //class Mouse
    class Mouse extends javax.swing.JFrame
    {




    //constructor, add mouse motion listener as inner class
    Mouse()
    {
    addMouseMotionListener
    (
    new javax.swing.event.MouseInputAdapter()
    {

    //mouseMoved
    public void mouseMoved(java.awt.event.MouseEvent a)
    {
    System.out.println("MouseInputAdapter.mouseMoved");
    mouseMoved(a.getX(),a.getY()); //not accepted
    // houseMoved(a.getX(),a.getY()); //accepted alternative
    }});
    setDefaultCloseOperation(javax.swing.JFrame.EXIT_ON_CLOSE);
    setSize(100,100);
    setVisible(true);
    }




    //mouseMoved, method called from inner class. Rejected
    void mouseMoved(int a,int b)
    {
    System.out.println("Mouse.mouseMoved");
    }




    //houseMoved, method called from inner class. Accepted
    void houseMoved(int a,int b)
    {
    System.out.println("Mouse.houseMoved");
    }




    //main
    public static void main(String[]a)
    {
    new Mouse();
    }}
     
    Martijn Mulder, Jul 1, 2005
    #1
    1. Advertising

  2. Martijn Mulder

    Daniel Dyer Guest

    On Fri, 01 Jul 2005 16:49:43 +0100, Martijn Mulder <i@m> wrote:

    > From within an inner class I call a method in the enclosing
    > class with the same name but WITH A DIFFERENT SIGNATURE. The
    > compiler (jikes) complains:
    >
    >
    >
    > Found 1 semantic error compiling "./Mouse.java":
    >
    > 25. mouseMoved(a.getX(),a.getY());
    > <--------------------------->
    > *** Error: The method "void mouseMoved(int a, int b);" contained in
    > the enclosing type "Mouse" is a perfect match for this method call.
    > However, it is not visible in this nested class because a method
    > with the same name in an intervening class is hiding it.**
    >
    >
    > Why is this? The name is the same but the signature is not.
    > A slight change in the name (houseMoved instead of mouseMoved)
    > makes it all work. What is the 'intervening class' jikes mouns
    > about?
    >


    Try javac, see if you get a less cryptic message. Then try the following:

    Mouse.this.mouseMoved(a.getX(),a.getY());

    Dan.

    --
    Daniel Dyer
    http://www.dandyer.co.uk
     
    Daniel Dyer, Jul 1, 2005
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Yamin
    Replies:
    4
    Views:
    16,848
    Yamin
    Oct 24, 2004
  2. E11
    Replies:
    1
    Views:
    4,948
    Thomas Weidenfeller
    Oct 12, 2005
  3. Ian
    Replies:
    2
    Views:
    366
    Ulrich Petri
    Sep 6, 2003
  4. Pyenos
    Replies:
    2
    Views:
    420
    Pyenos
    Dec 27, 2006
  5. aidy
    Replies:
    5
    Views:
    256
    Marcin Mielżyński
    Jun 4, 2006
Loading...

Share This Page