Amikor a gc begyűjti az objektumot, akkor garantáltan lefut a finalizer, az nem biztos, hogy a program futása alatt a gc el fog ide jutni.
pl.:
import java.util.Objects;
class Test {
private String instanceId;
protected Test(String instanceId) {
this.instanceId = Objects.requireNonNull(instanceId);
}
protected void finalize() {
System.out.println(instanceId);
}
public static void main(String args[]) {
Test instance = new Test("Instance #1");
instance = null;
System.gc();
instance = new Test("Instance #2");
}
}
BlackY
--
"en is amikor bejovok dolgozni, nem egy pc-t [..] kapcsolok be, hanem a mainframe-et..." (sj)