I am reading Thinking in Java, 4th Edition. I found a problem: when I tested the source code in Eclipse IDE, I found the results are different. I hope someone can help me!
Here is the source code
class Book {
boolean checkedOut = false;
Book(boolean checkOut) {
checkedOut = checkOut;
}
void checkIn() {
checkedOut = false;
}
protected void finalize() {
if(checkedOut)
System.out.println("Error:checked out");
//Normally,you'll also do this:
//super.finalize();//Call the base-class version
}
}
public class TerminationCondition {
public static void main(String[]args) {
Book novel=new Book(true);
//Proper cleanup:
novel.checkIn();
//Drop the reference,forget to clean up:
new Book(true);
new Book(true);
new Book(true);
//Force garbage collection & finalization:
System.gc();
}
}
The result of the Book:
Error: Checked out
The result of IDE
(nothing)
The version of Java I use:
Java version "1.7.0_51"
Java(TM) SE Runtime Environment (build 1.7.0_51-b13)
Java HotSpot(TM) 64-Bit Server VM (build 24.51-b03, mixed mode)
The version of Java in the Book is Java 5. Did something about finalize method change?