English 中文(简体)
Java method help [duplicate]
原标题:
This question already has answers here:
Closed 10 years ago.

Possible Duplicate:
Static method in java

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!

问题回答

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()

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.

You can also try:

LibraryFunctions lf = new LibraryFunctions();
lf.addSong();

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()

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.

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.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签