无法调用非静态方法 [英] Can't call non static method
本文介绍了无法调用非静态方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用此 wavRead(文件名)
但是收到消息无法对非静态方法进行静态引用
。
I am trying to use this wavRead(filename)
but am getting the message cannot make a static reference to a non static method
.
我可以简单地将其设为静态并解决我的问题,但如果不走那条路线怎么办呢。我想保持方法不是静态的。
I could simply make it static and that solves my problem, but how would do it without going that route. I would like to keep the method non static.
这里有一些代码可以让你看到最新情况:
Here is a bit of the code to let you see whats going on:
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);
}
这是一个实例方法,它需要一个实例才能访问它。请浏览关于类和对象的官方教程。
It's an instance method, it requires an instance to access it. Please go through the official tutorials on classes and objects.
这篇关于无法调用非静态方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文