finalize() newbie question

From: ark (arkhas_at_comcast.net)
Date: 03/24/04


Date: Wed, 24 Mar 2004 16:42:28 GMT

Dear gurus,
Could you explain why finalize() is not called when the "a" reference goes
out of scope?
Thanks a lot,
Ark
Here is the code:

class AClass {
 public void finalize() {
    System.out.println("Done with AClass");
 }
 static void foo() {
   AClass aa = new AClass();
 }
}

public class First {
 static public void main(String[] args) {
  {
   AClass.foo();
  }
  System.gc();//calls finalize()
  {
   new AClass();
  }
  System.gc();//calls finalize()
  {
   AClass a = new AClass();
  }
  System.gc();//doesn't call finalize()
 }
}



Relevant Pages

  • Re: finalize()
    ... I believe the spec states that there is no guarentee that finalize ... public void doSomething() throws Exception { ...
    (comp.lang.java.programmer)
  • Re: Java Question
    ... As you know finalize has to be ... public void finalize(){System.out.println;} ... public void extraFinalizeStuff(){ ... Note however, this approach is only ever going to enforce that the implementing derived classes implement the template method, it doesn't help for further derived classes below that. ...
    (comp.lang.java.programmer)
  • Re: Java Question
    ... But below is my class hirearchy. ... As you know finalize has to be ... > public void finalize(){ ... > abstract class B extends A { ...
    (comp.lang.java.programmer)