Can a static reference be made to a non-static method

0 votes

I have a class named md which has a method named setLI:

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

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

public void loanItem() {

But I am getting the error

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

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

Jun 25, 2018 in Java by parth295
• 4,600 points

1 answer to this question.

0 votes

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(Md m) {
answered Jun 26, 2018 by samarth295
• 2,190 points

Related Questions In Java

0 votes
2 answers

How a static map can be initialized?

public class Test { ...READ MORE

answered Sep 12, 2018 in Java by Sushmita
• 6,850 points
0 votes
1 answer

How can one day be added to a date?

One possible solution could be using calendar ...READ MORE

answered Jun 8, 2018 in Java by Daisy
• 8,040 points
0 votes
2 answers

How can I convert a String variable to a primitive int in Java

 Here are two ways illustrating this: Integer x ...READ MORE

answered 3 hours ago in Java by Sirajul
• 17,700 points
0 votes
1 answer

How can a war file be deployed in Tomcat 7?

You can access your application from: http://localhost:8080/sample Deploying ...READ MORE

answered Apr 20, 2018 in Java by sophia
• 1,400 points
0 votes
1 answer

Non-static variable cannot be referenced from a static context. Explain?

Static fields and methods are connected to ...READ MORE

answered May 21, 2018 in Java by parth295
• 4,600 points
0 votes
2 answers
0 votes
1 answer

Why “non-static method cannot be referenced from a static context”?

You can't call something that doesn't exist. ...READ MORE

answered Oct 17, 2018 in Java by sharth
• 3,320 points
0 votes
1 answer
+2 votes
12 answers

“A Non-static method cannot be referenced from a static context” error

this method must be static so that it will exist even though the ...READ MORE

answered Jun 11 in Java by Neha
• 330 points
0 votes
3 answers

How to read a Text File in Java?

You can use readAllLines and the join method to ...READ MORE

answered Jul 27, 2018 in Java by samarth295
• 2,190 points