theclapp

Search theclapp.blog-city.com

 

Chapter 4, ex 12, Java only

posted Wednesday, 15-Jan-2003
09:40 -- starting 4.12, Java version
09:43 -- Done
---
// ch04_ex12.java

class 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();
}
}
---

Got the message this time.

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.

links: digg this    del.icio.us    technorati    reddit