Question

I am trying to make a Java package 'mylib' with classes Library{} and Book{}.

Here is the code for class Library{}:

/*
 Create collection of books
 List books and status
 User input:
   'B' - Borrow a book
   'R' - Reserve a book
   'I' - Return a book
   'X' - Exit program
*/

package mylib;

public class Library {

    public static void main(String[] args) {
        Book[] MyBooks = new Book[3];
        Book x;

        MyBooks[0] = new Book("The Lover's Dictionary", "Levithan, D.", 211);
        MyBooks[1] = new Book("White Tiger", "Adiga, A.", 304);
        MyBooks[2] = new Book("Thirteen R3asons Why", "Asher, J.", 336);

        for (int i = 0; i < MyBooks.length; i++) {
            x = MyBooks[i];
            System.out.println((i + 1) + " " + x.sTitle);
        }
    }
}

Here is the code for class Book{}:

package mylib;

class Book {
    // Declare fields
    byte iStatus;
    int iPages;
    String sTitle, sAuthor;
    String sBorrowedBy, sReservedBy;
    String sDueDate, sReturnDate;

    public static final byte BORROWED = 0, AVAILABLE = 1, RESERVED = 2;

    // Constructor
    public Book(String Title, String Author, int Pages) {
        this.sTitle = Title;
        this.sAuthor = Author;
        this.iPages = Pages;
        this.iStatus = this.AVAILABLE;
    }

    // Borrow method
    static void borrowBook(String Borrower, String Due) {
        if (this.iStatus == this.AVAILABLE) {
            this.sBorrowedBy = Borrower;
            this.sDueDate = Due;
            this.iStatus = this.BORROWED;
        } else if (this.iStatus == this.RESERVED
                && this.sReservedBy == Borrower) {
            this.sBorrowedBy = Borrower;
            this.sDueDate = Due;
            this.sReservedBy = "";
            this.iStatus = this.BORROWED;
        }
    }

    /*
     * static int reserveBook(String Borrower) {
     * 
     * }
     * 
     * static void returnBook(String Return) {
     * 
     * }
     */
}

The partial codes above are given by the professor. I commented out the empty methods and tested the program just to see if it would compile.

I am having 14 errors with the this keyword. Any help?

Was it helpful?

Solution 3

Since you are using this in a static context it is giving you the error. Try the following code:

package myLib;

class Book {
// Declare fields
byte iStatus;
int iPages;
String sTitle, sAuthor;
String sBorrowedBy, sReservedBy;
String sDueDate, sReturnDate;

public static final byte BORROWED = 0, AVAILABLE = 1, RESERVED = 2;

// Constructor
public Book(String Title, String Author, int Pages) {
    this.sTitle = Title;
    this.sAuthor = Author;
    this.iPages = Pages;
    this.iStatus = Book.AVAILABLE;
}

// Borrow method
//Remove the static keyword
//Refer to the remaining static variables like AVAILABLE OR BORROWED using Book and not this keyword.
 void borrowBook(String Borrower, String Due) {
    if(this.iStatus == Book.AVAILABLE) {
        this.sBorrowedBy = Borrower;
        this.sDueDate = Due;
        this.iStatus = Book.BORROWED;
    }
    else if(this.iStatus == Book.RESERVED && this.sReservedBy == Borrower) {
        this.sBorrowedBy = Borrower;
        this.sDueDate = Due;
        this.sReservedBy = "";
        this.iStatus = Book.BORROWED;
    }

    }
}

OTHER TIPS

in this method

static void borrowBook(String Borrower, String Due) {

You cannot use this in a static context

As I see ,there is no need of making that method static.

Prefer to read Understanding Instance and Class Members

You cannot use this in static context , like static methods. Why are you making your borrowBook() method as static. It should be an instance method without the static keyword.

static methods belong to the class and is shared by all its instances. You can invoke them directly using the classname like Book.borrowBook(....) , if this happens the run time will have no idea what/which object this refers to in that context .

Read JLS.3 15.8

The keyword this may be used only in the body of an instance method, instance initializer, or constructor, or in the initializer of an instance variable of a class. If it appears anywhere else, a compile-time error occurs.


In your case better to make the borrowBook() method an instance method as it changes the state of the invoking object i.e. modifies its attributes.Just change the method declaration and remove static:

void borrowBook(String Borrower, String Due) {....... }

I suggest you to use IDE to coding since this issue will understand by your own. because error is here use this in a static block.

this keyword is used to refer non-static variables from non-static methods. You are referring to non-static variables from static method.

just change this line :

static void borrowBook(String Borrower, String Due) {

to

public void borrowBook(String Borrower, String Due) {
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top