package pkg;
class Book{
boolean checkedOut=false;
Book(boolean checkOut){
checkedOut=checkOut;
}
void checkIn() {
checkedOut=false;
}
protected void finalize() {
if (checkedOut)
System.out.println("Error,there a book is checked out without check in.");
}
}
public class p89 {
public static void main(String[] args) {
//typical borrow
Book novel=new Book(true);
novel.checkIn();
//forget to return the book
new Book(true);
new Book(true);
new Book(true);
new Book(true);
new Book(true);
System.gc();
}
}
output:
Error,there a book is checked out without check in.
Error,there a book is checked out without check in.
Error,there a book is checked out without check in.
Error,there a book is checked out without check in.
Error,there a book is checked out without check in.
【总结】
System.gc();可以被用于检查编译错误之外的“业务逻辑错误”