• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Having Trouble Referencing a Method from Anonther Method in Another Class

 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic