简体   繁体   中英

I cant access a class I defined from the main loop? [SOLVED]

I have an extremely simple piece of code where I am calling a method that I defined, and it wont run because it says I haven't defined the method?

class Main {
  public static void main(String[] args) {
go(7,3);
  }

} /// in a separate file named go.java -->

 class go{
public static int go(int x, int y){
 if(x <= 1)
     return y;
  else
      return go(x - 1, y) + y;

} }

You have two options, use the class name go to identify the class containing the method

public static void main(String[] args) {
    go.go(7,3);
}

or static import it like

import static go.go;
public class Main {
    public static void main(String[] args) {
        go(7,3);
    }
}

First thing first, Class name should start with upper case, your another class should be Go.java , to access static method Refer Class name that is Go.go(7,3);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM