public class Test { public static void main(String[] args) throws Exception { Book b=new Book(true); b.checkIn(); new Book(true); System.gc(); } } class Book{ boolean checkdOut = false; Book (boolean checkOut) { checkdOut = checkOut; } protected void checkIn() { checkdOut = false; } protected void finalize() { if (checkdOut) { System.out.println("Error : check Out"); } } }
在执行回收机制之前会先执行finalize();