我是java的初学者。
我无法编译下面的代码,因为我有17个关于“无法从静态上下文中引用的非静态变量”的错误。 它总是指向“这个”。声明。 我是一名学生非静态变量不能从静态上下文中引用
package MyLib;
import java.util.*;
class Book {
static int pages;
static String Title;
static String Author;
static int status;
static String BorrowedBy;
static Date ReturnDate;
static Date DueDate;
public static final int
BORROWED = 0;
public static final int
AVAILABLE = 1;
public static final int
RESERVED = 2;
//constructor
public Book (String Title, String Author, int pp) {
this.Title = Title;
this.Author = Author;
this.pages = pp;
this.status = this.AVAILABLE;
}
public static void borrow(String Borrower/*, Date Due*/){
if (this.status=this.AVAILABLE){
this.BorrowedBy=Borrower;
this.DueDate=Due;
}
else {
if(this.status == this.RESERVED && this.ReservedBy == Borrower){
this.BorrowedBy= Borrower;
this.DueDate=Due;
this.ReservedBy="";
this.status=this.BORROWED;
}
}
}
哇,自从这个问题出现在'SO'后只有5分钟 – Reimeus
您的借用方法是否需要静态?我觉得使它非静态可以解决你的问题... – StephenTG
让你觉得也许java IDE可能会提醒你。 – sje397