my dog learned polymorphism
The moose likes Beginning Java and the fly likes returning a value Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "returning a value" Watch "returning a value" New topic

returning a value

Erkki Veps�l�inen

Joined: Feb 10, 2004
Posts: 9
What is the reason that I get an error message "non-static method cannot be referenced from a static context" while trying to return a value from a class? what does it mean actually?
Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24199

Non-static methods -- i.e., regular instance methods -- have to have an object of the class to operate on. Static methods don't need an object. Therefore, you can't call a non-static method from a static one without creating an object. Here's an example of code that will cause this error message:

And here's how you would fix the problem:

[Jess in Action][AskingGoodQuestions]
Erkki Veps�l�inen

Joined: Feb 10, 2004
Posts: 9
Thanks for informing! Got it clear!
I agree. Here's the link:
subject: returning a value
It's not a secret anymore!