1、调用垃圾回收机制,以及finalize用法
class Book{
boolean checkedOut = false;
Book(boolean checkOut){
checkedOut = checkOut;
}
void checkIn(){
checkedOut = false;
}
public void finalize(){
if(checkedOut){
System.out.println("Error:checked out!");
}
}
}
public class Assignment {
public static void main(String[] args) {
Book novel = new Book(true);
novel.checkIn();
new Book(true);
System.gc();
}
}
运行结果:
Error:checked out!