76

I have a class named Media which has a method named setLoanItem:

public void setLoanItem(String loan) {
    this.onloan = loan;
}

I am trying to call this method from a class named GUI in the following way:

public void loanItem() {
    Media.setLoanItem("Yes");
}

But I am getting the error

non-static method setLoanItem(java.lang.String) cannot be referenced from a static context

I am simply trying to change the variable onloan in the Media class to "Yes" from the GUI class.

I have looked at other topics with the same error message but nothing is clicking!

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
Daniel Mckay
  • 777
  • 1
  • 5
  • 3
  • 6
    you should accept an answer for this. – NateW Sep 26 '15 at 18:13
  • 2
    Dup-hammer wielders please note that *"Non-static **method** cannot be referenced ..."* and *"Non-static **variable** cannot be referenced ..."* are different and require different answers. Thanks. – Stephen C Jun 30 '18 at 00:18

4 Answers4

75

Instance methods need to be called from an instance. Your setLoanItem method is an instance method (it doesn't have the modifier static), which it needs to be in order to function (because it is setting a value on the instance that it's called on (this)).

You need to create an instance of the class before you can call the method on it:

Media media = new Media();
media.setLoanItem("Yes");

(Btw it would be better to use a boolean instead of a string containing "Yes".)

Nathan Hughes
  • 94,330
  • 19
  • 181
  • 276
  • I get a null pointer exception when I do this though...What do you think the problem is? Let me know if you need any code. Thanks! (Been stuck on this for 3 and a half hours now!) – Ruchir Baronia Dec 10 '15 at 05:48
  • @Ruchir: see http://stackoverflow.com/questions/218384/what-is-a-null-pointer-exception-and-how-do-i-fix-it – Nathan Hughes Dec 10 '15 at 12:26
12

setLoanItem is an instance method, meaning you need an instance of the Media class in order to call it. You're attempting to call it on the Media type itself.

You may want to look into some basic object-oriented tutorials to see how static/instance members work.

Joe Enos
  • 39,478
  • 11
  • 80
  • 136
2

setLoanItem() isn't a static method, it's an instance method, which means it belongs to a particular instance of that class rather than that class itself.

Essentially, you haven't specified what media object you want to call the method on, you've only specified the class name. There could be thousands of media objects and the compiler has no way of knowing what one you meant, so it generates an error accordingly.

You probably want to pass in a media object on which to call the method:

public void loanItem(Media m) {
    m.setLoanItem("Yes");
}
Michael Berry
  • 70,193
  • 21
  • 157
  • 216
0

You need to correctly separate static data from instance data. In your code, onLoan and setLoanItem() are instance members. If you want to reference/call them you must do so via an instance. So you either want

public void loanItem() {
    this.media.setLoanItem("Yes");
}

or

public void loanItem(Media object) {
    object.setLoanItem("Yes");
}

depending on how you want to pass that instance around.

OrangeDog
  • 36,653
  • 12
  • 122
  • 207