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() {
    Md.setLI("Yes");
}

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,590 points
39 views

1 answer to this question.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.
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) {
    m.setLI("Yes");
}
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,820 points
42 views
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,020 points
16 views
0 votes
1 answer

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

You can convert a String to int by using a function: int ...READ MORE

answered Apr 12, 2018 in Java by sharth
• 3,310 points
43 views
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
411 views
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,590 points
3,105 views
0 votes
1 answer

Why are you not able to declare a class as static in Java?

Declare your class like this: public class eOAuth ...READ MORE

answered Oct 4, 2018 in Java by Daisy
• 8,020 points
33 views
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,310 points
109 views
0 votes
1 answer
+2 votes
11 answers

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

Instead of final, here you can wrap ...READ MORE

answered Jan 29 in Java by zemiak
23,312 views
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
35 views

© 2018 Brain4ce Education Solutions Pvt. Ltd. All rights Reserved.
"PMP®","PMI®", "PMI-ACP®" and "PMBOK®" are registered marks of the Project Management Institute, Inc. MongoDB®, Mongo and the leaf logo are the registered trademarks of MongoDB, Inc.