java - "Non-static method cannot be referenced from a static context" error -


this question has answer here:

i have class named media has method named setloanitem:

public void setloanitem(string loan) {     this.onloan = loan; } 

i trying call method class named gui in following way:

public void loanitem() {     media.setloanitem("yes"); } 

but getting error

non-static method setloanitem(java.lang.string) cannot referenced static context

i trying change variable onloan in media class "yes" gui class.

i have looked @ other topics same error message nothing clicking!

instance methods need called instance. setloanitem method instance method (it doesn't have modifier static), needs in order function (because setting value on instance it's called on (this)). need create instance of class before can call method on it:

media media = new media(); media.setloanitem("yes"); 

(btw better use boolean instead of string containing "yes".)


Comments

Popular posts from this blog

Delphi Wmi Query on a Remote Machine -