参见英文答案 > cannot make a static reference to a non static method 5个
我正在尝试使用此wavRead(文件名),但我得到的消息无法对非静态方法进行静态引用.
我可以简单地让它静止,并解决我的问题,但如果不走那条路,怎么办呢.我想保持方法非静态.
这里有一些代码可以让你看到发生了什么:
public class Sound {
double [] mySamples;
public static void main(String[] args){
String filename = null;
System.out.println("Type the filename you wish to act upon.");
Scanner scanIn = new Scanner(System.in);
filename = scanIn.next();
wavRead(filename);
}
public void wavRead(java.lang.String fileName){
mySamples = WavIO.read(fileName);
}
解决方法:
创建一个类的实例
public static void main(String[] args){
String filename = null;
System.out.println("Type the filename you wish to act upon.");
Scanner scanIn = new Scanner(System.in);
filename = scanIn.next();
Sound sound = new Sound();
sound.wavRead(fileName);
}
标签:java,methods,static
来源: https://codeday.me/bug/20191006/1859558.html