-2

so I want to call this method from another class which is a non-static method that requires an argument of a string. However when I try to enter an argument into the parameter for the book.borrow method it says that I "cannot make a static reference to a non-static method". What am I doing wrong here?

import java.util.Scanner;

public class LibrarySystem{
    public static void main(String[]args){
        book book1 = new book("Divergent", "Veronica Roth", "B001", null);
        book book2 = new book("Green Eggs and Ham", "Dr Seuss", "B002", null);
        System.out.printf("%s\n", book1.title.toString()+ " " + (book1.bookID.toString()));
        System.out.printf("%s\n", book2.title.toString()+ " "  + (book2.bookID.toString()));

        Scanner students = new Scanner(System.in);
        String student  = String.valueOf(students);
        book.borrow(student);
    }
}

Underneath has the method that I want to call

public boolean borrow(String borrowerID) {
    if (bookID != null) {
        this.borrowerID = borrowerID;
        return true;
    } else {
        return false;
    }
}
khelwood
  • 55,782
  • 14
  • 81
  • 108

2 Answers2

0
book.borrow(student);

above code snippet would have been valid if borrow method was a static method.

Since its not-static, you can only call it on an instance of book class.

book1.borrow(student);
Yousaf
  • 27,861
  • 6
  • 44
  • 69
0

If a method is not declared static, the method must be called on an instance of the object, not a static reference. You must initialize the class containing the method, then run borrow().

Bradley H
  • 339
  • 1
  • 7