aspose file tools*
The moose likes Beginning Java and the fly likes Having Trouble Referencing a Method from Anonther Method in Another Class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Having Trouble Referencing a Method from Anonther Method in Another Class" Watch "Having Trouble Referencing a Method from Anonther Method in Another Class" New topic
Author

Having Trouble Referencing a Method from Anonther Method in Another Class

Toshiro Hitsuguya
Greenhorn

Joined: May 15, 2009
Posts: 19
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

Joined: Jul 08, 2003
Posts: 24187
    
  34

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:



[Jess in Action][AskingGoodQuestions]
Toshiro Hitsuguya
Greenhorn

Joined: May 15, 2009
Posts: 19
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

Joined: May 15, 2009
Posts: 19
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
Sheriff

Joined: Sep 28, 2004
Posts: 19061
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Having Trouble Referencing a Method from Anonther Method in Another Class