Can't call non static method
Solution 1
Create an instance of your class
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.
Solution 2
You cannot call non-static methods or access non-static fields from main
or any other static method, because non-static members belong to a class instance, not to the entire class.
You need to make an instance of your class, and call wavRead
on it, or make wavRead
and mySamples
static:
public static void main(String[] args) {
Sound instance = new Sound();
...
instance.wavRead(fileName);
}
Solution 3
You need to make a Sound
object before you can call wavRead
on it. Something like
Sound mySound = new Sound();
mySound.wavRead(filename);
Static just means that you don't need to have an instance of the class that the method belongs to.
Comments
-
erp almost 4 years
I am trying to use this
wavRead(filename)
but am getting the messagecannot 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); }