views:

87

answers:

7
+1  Q: 

Java method help

Ok, so I'm working on a project for a class I'm taking.. simple music library. Now I'm having some issues, the main issue is I'm getting "non-static method cannot be referenced from a static context"

Here is a function I have

public void addSong() {
    Scanner scan = new Scanner(System.in);
    Song temp = new Song();
    int index = countFileLines(Main.databaseFile);
    index = index + 2;
    temp.index = index;
    System.out.print("Enter the artist name: ");
    temp.artist.append(scan.next());
}

Now thats in a class file called LibraryFunctions. So I can access it with LibraryFunctions.addSong();

Now I'm trying to run this in my main java file and its giving me the error, I know why the error is happening, but what do I do about it? If I make addSong() a static function then it throws errors at me with the Song temp = new Song() being static. Kind of ironic.

Much help is appreciated on this!

+1  A: 

If you want to call it as LibraryFunctions.addSong(), it needs to have the signature public static void addSong().

More info:
Only static methods can be called without instantiating a class first.

R. Bemrose
He tried that but got a further error.
vickirk
+3  A: 

Follow these simple rules:

  1. If it's a static method call it with ClassName.methodName()
  2. If it's a non-static method call it with classInstance.methodName()
Yuval A
While good advice I believe this is what the op was following. I think the op's issue is related to inexperience of dealing with nested classes, difference between static/non static nested classes is the only explanation of BOTH his errors.
vickirk
+1  A: 

You can also try:

LibraryFunctions lf = new LibraryFunctions();
lf.addSong();
Pablo Santa Cruz
+1  A: 

Well you have two options really:

  1. Change addSong() to static and reference Song through it's static members if possible.
  2. Create a new instance of LibraryFunctions and then use the non-static method addSong()
James
+1  A: 

I take that your class Song is a non static nested class? e.g.

class LibraryFunctions {

    class Song {
       // ...
    }

}

If so you can either make it a static nested class, or lift the Song class into a separate class.

vickirk
A: 

In terms of structure, may I suggest that the LibraryFunctions class file be turned into a MusicLibrary class? That way, in your main application code, you can instantiate a MusicLibrary every time the code runs. It will also make it easier to separate static functions and instance functions, which would probably solve your issue right now.

public class MusicManager {

    public static void main(String[] args) {
        MusicLibrary myMusic = new MusicLibrary();
        myMusic.addSong();
        // other stuff
    }
}

Then MusicLibrary:

public class MusicLibrary {

    public MusicLibrary() {
    }

    public void addSong() {
        Scanner scan = new Scanner(System.in);
        Song temp = new Song();
        int index = countFileLines(Main.databaseFile);
        index = index + 2;
        temp.index = index;
        System.out.print("Enter the artist name: ");
        temp.artist.append(scan.next());
    }
}

And finally, I would put the class Song outside of MusicLibrary so that you can reuse it later.

Another added benefit of this is that you can make MusicLibrary implement Serializable and save the library to a file. Plus you could place an array of MusicLibraries inside of a MusicLibrary and have playlists. All kinds of options.

David Antaramian
A: 

Have a look at this post

Adriaan Koster