File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Having Trouble Referencing a Method from Anonther Method in Another Class

 
Toshiro Hitsuguya
Greenhorn
Posts: 19
  • 0
  • 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?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • 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
  • 0
  • 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
  • 0
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 20836
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • 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
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic