Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Having Trouble Referencing a Method from Anonther Method in Another Class

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am currently having trouble with a project I am working on. It is to create a simple system for a library.

Here is my code for the library class:


This is the class I am trying to refer to:



With all of that. I have compiled Book.java fine. However, whenever I try to compile Library, I get a "non-static method can be referred from a static context". The library methods are not static.

My question is how can I rearrange the code in my library class to reference borrowed() and returned() in my book class?
 
author and iconoclast
Posts: 24203
43
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you borrow a book from the library or return a book to the library, it's an action you perform relative to a specific book; it makes no sense to return a book without referring to a specific book, right? The Library doesn't know which book you're returning or borrowing, which leaves it up to you, the borrower -- the person who calls the methods. So I think the methods of Library need to accept the book as a parameter:

 
Toshiro Hitsuguya
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason I need to do this is because later I have to evoke the method in a format like this firstLibrary.borrowBook("The Lord of the Rings").
 
Toshiro Hitsuguya
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the information. I tried that and I am still getting "a non-static method cannot be referenced from a static context" error message. Also, how would I write those method if I have evoke the method for multiple books?

Thank you.
 
author
Posts: 23879
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I thought about continuing EFH analogy about how you need an instance of library to borrow a book -- hence, you won't know where to borrow it from. etc. But...

Unless, you have an understanding of static and non-static context, and what is in scope during what context, you will keep get these error messages. So... I recommend that you go back and review the chapter on methods. You need to review the difference between static and non-static methods, and how, when do you use them.

Henry
 
It's never done THAT before. Explain it to me tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
    Bookmark Topic Watch Topic
  • New Topic