Call A Method From Within Inner Class

M

Martijn Mulder

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();
}}
 
D

Daniel Dyer

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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top