---Got the message this time.
// ch04_ex12.javaclass ch04_ex12 {
int i;public void finalize() {
System.out.println( "Finalizing object." );
}public static void main(String[] args) {
ch04_ex12 obj = new ch04_ex12();
obj = null;
System.gc();
}
}
---
One intesting subtlety. If I said
{
ch04_ex12 obj = new ch04_ex12();
}
System.gc();instead of actually assigning null to obj, the gc didn't finalize the object. I tried the same thing in another function, and then the gc in main() did finalize that object: static void alloc() { ch04_ex12 obj = new ch04_ex12(); } public static void main(String[] args) {
alloc();
{
ch04_ex12 obj = new ch04_ex12();
}
System.gc();
}
Prints "Finalizing object.", but only once. Calling System.runFinalization() doesn't help.