无法从静态contex java引用非静态方法openInputFile(字符串)

无法从静态contex java引用非静态方法openInputFile(字符串),java,static,Java,Static,这是我的问题代码: public File openInputFile(Scanner kb) { if(kb == null) { throw new RuntimeException("ERROR! Scanner Not Detected."); } System.out.print("Enter Name of File: "); String fn = kb.nextLine(); File inf = FileUtil.openInputFil

这是我的问题代码:

public File openInputFile(Scanner kb)
{
  if(kb == null)
  {   
     throw new RuntimeException("ERROR! Scanner Not Detected.");
  }
  System.out.print("Enter Name of File: ");
  String fn = kb.nextLine();
  File inf = FileUtil.openInputFile(fn); 
  return inf;
}
我得到以下错误:

FileUtil.java:33: error: non-static method openInputFile(String) cannot be referenced from a static context
      File inf = FileUtil.openInputFile(fn); 
                         ^

没有任何东西是静态的,所以我不确定为什么会出现这个错误。有人能帮忙吗?

File inf=FileUtil.openInputFile(fn)
告诉编译器
openInputFile(fn)
是类
FileUtil
的静态方法,因为您试图从类名本身调用该方法。这就是为什么编译器说

FileUtil.java:33:错误:无法从静态上下文引用非静态方法openInputFile(字符串)


因此,您所需要做的就是创建一个file util对象并从该对象调用该方法。

FileUtil
类有一个方法
openInputFile()
,该方法似乎是非静态的,因此您必须通过创建
FileUtil
的实例来调用它

FileUtil fileut= new FileUtil();
fileut.openInputFile();

您使用它的方式告诉编译器它是静态的。当方法是静态的时,您将使用它作为类的名称。方法的名称甚至不清楚您试图通过代码实现什么。op在方法openInputFileYes中使用openInputFile方法,但这些方法似乎被重写,因为它们有不同的参数,但听起来很混乱,不能表达程序员试图在这里完成什么?!!!!嗯,没错,但我认为在任何情况下,这都是可行的,即使这两个方法,
openInputFile(scanner)
openInputFile(String)
是同一类的一部分(重写方法)。或者,
openInputFile(scanner)
方法存在于完全不同的类中。但是是的,它确实不清楚OP想要做什么achieve@KickButtowski这看起来确实令人困惑,但问题不同。看起来更像是设计问题,而不是编译器错误。op在方法openInputFileParam类型中使用openInputFile方法。FileUtil方法接受字符串,而op的方法具有类型为“Scanner”的参数`