1

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!

0

6 Answers 6

3

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()
Sign up to request clarification or add additional context in comments.

1 Comment

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.
1

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.

1 Comment

He tried that but got a further error.
1

You can also try:

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

Comments

1

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

Comments

1

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.

Comments

0

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.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.