Search code examples
javaawtawtrobot

Error telling me that the method in my run class is undefined even though it is


I was trying to get into the Java robot class so I thought I would at first just simply write a program to move the mouse to 0,0 on the screen.

It all looks perfect but when I try to run it I get an error:

"The method go() is undefined for the type run"

Wondering if any of you know why I was getting this error.

main.java:

public class main {

    public static void main(String[] args) {
        run run = new run();

        run.go();
    }
}

run.java:

import java.awt.AWTException;
import java.awt.Robot;
public class run {

    public void go(){

        Robot robot = null;
        try {
            robot = new Robot();
        } catch (AWTException e) {
            e.printStackTrace();
        }

        robot.mouseMove(0, 0);
    }


}

-Thanks


Solution

  • The type of your class is run, and that is what the compiler is attempting to use (the class named run) and there is no static void go. Basically, you have shadowed run (I note it is not in a package, and class names should start with a capital letter). I suggest you fix those things, but you could change

    run run = new run();
    run.go();
    

    to

    new run().go();
    

    As for the shadow, the variable named run is shadowed by the class named run (lexically class names are searched before variable names).

    run runner = new run();
    runner.go();
    

    would also work.