Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

returning a value

 
Erkki Veps�l�inen
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
Thanks,
Erkki
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
Erkki Veps�l�inen
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for informing! Got it clear!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic