Ask you about the finalize () method of the problem

Category: Java SE
2012-02-09 12:24:16

Sponsored Links
Brother I read recently is >> << java programming ideas, according to the title of the book to do the exercises, but to learn about the finalize () method when there is a little problem, I hope you have educated us
original code as follows :

public class Default {
public static void main (String args []) {
NoConstruct a = new NoConstruct () ;
System.out.println (a.status);
System.gc ();

class NoConstruct {
String status = "fill";
public NoConstruct () {

public void setempty () {
status = "empty";

public void setfill () {
status = "fill";

public void finalize () {
if (status == "fill") {
System.out.println ("can 't destroy it ");
operating results as:
performed according to the understanding of younger system.gc () should be performed after NoConstruct class finalize () method, the output should be:
can't destroy it
why would not perform finalize () method do?

Sponsored Links

2012-02-09 12:36:25
Prictical java page 236
in garbage collected before a class object, JVM will call this class finalize ()
but can not guarantee finalize () the execution time can be expected. This is due to the "end (finalization) action" and "garbage collection" between the asynchronous nature caused. By inference, finalize () may not be executed before the program ends
2012-02-09 12:41:18
But I performed system.gc () This instruction should be enforced "garbage collection" feature.
I looked at, because not belong to a "junk." Fainting. . . . . .
Domain and server ip had changed since 8/23/2013. Suspend the user registration and posts for program maintenance.