Java non static method playCompletely cannot be referenced from a static context
I've been trying to create a method (in a separate class) that takes a String as a parameter and uses a premade SoundEngine (a different class) object to play that file inputted as a string. This is the code I have so far. The problem comes up with
SoundEngine.playCompletely(file);import java.util.ArrayList;
public class AudioFileList
{
// Field for a quantity of notes.
private ArrayList<String> files;
// Constructor that initializes the array field.
public AudioFileList()
{
files = new ArrayList<String>();
}
// Method to add file names to the collection.
public void addFile(String file)
{
files.add(file);
}
// Method to return number of files in the collection.
public int getNumberOfFiles()
{
return files.size();
}
// Method to print the strings in the collection.
public void listFiles()
{
int index = 0;
while(index < files.size())
{
System.out.println( index + ":" + files.get(index));
index++;
}
}
// Method that removes an item from the collection.
public void removeFile(int fileNumber)
{
if(fileNumber < 0)
{
System.out.println ("This is not a valid file number");
}
else if(fileNumber < getNumberOfFiles())
{
files.remove(fileNumber);
}
else
{
System.out.println ("This is not a valid file number");
开发者_如何学C }
}
// Method that causes the files in the collection to be played.
public void playFile(String file)
{
SoundEngine.playCompletely(file);
}
}
Any help is very much appreciated.
SoundEngine
's playCompletely
function is an instance function, not a class function. So instead of:
SoundEngine.playCompletely(file); // Compilation error
you want:
// First, create an instance of SoundEngine
SoundEngine se = new SoundEngine();
// Then use that instance's `playCompletely` function
se.playCompletely(file);
精彩评论