The Artima Developer Community
Sponsored Link

Java Answers Forum
the great puzzle of finalizing

1 reply on 1 page. Most recent reply: May 13, 2002 10:55 PM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
hongbo

Posts: 1
Nickname: floodwave
Registered: May, 2002

the great puzzle of finalizing Posted: May 13, 2002 4:55 AM
Reply to this message Reply
Advertisement
the codes are shown as below ,which is got from THINKING IN JAVA

class DoBaseFinalization {

public static boolean flag=false;

}

class Characteristic {

String s;

Characteristic(String c) {

s = c;

System.out.println(

"Creating Characteristic " + s);

}

protected void finalize() {

System.out.println(

"finalizing Characteristic " + s);

}

}

class LivingCreature {

Characteristic p =

new Characteristic("is alive");

LivingCreature() {

System.out.println("LivingCreature()");

}

protected void finalize() throws Throwable {

System.out.println(

"LivingCreature finalize");

// Call base-class version LAST!

if(DoBaseFinalization.flag)

super.finalize();

}

}

class Animal extends LivingCreature {

Characteristic p =

new Characteristic("has heart");

Animal() {

System.out.println("Animal()");

}

protected void finalize() throws Throwable {

System.out.println("Animal finalize");

if(DoBaseFinalization.flag)

super.finalize();

}

}

class Amphibian extends Animal {

Characteristic p =

new Characteristic("can live in water");

Amphibian() {

System.out.println("Amphibian()");

}

protected void finalize() throws Throwable {

System.out.println("Amphibian finalize");

if(DoBaseFinalization.flag)

super.finalize();

}

}

public class Frog extends Amphibian {

Frog() {

System.out.println("Frog()");

}

protected void finalize() throws Throwable {

System.out.println("Frog finalize");

if(DoBaseFinalization.flag)

super.finalize();

}

public static void main(String[] args) {

if(args.length != 0 &&

args[0].equals("finalize")){

// System.out.println(DoBaseFinalization.flag);

DoBaseFinalization.flag = true;}

else

{ //********

System.out.println("Not finalizing bases");}

new Frog(); // Instantly becomes garbage

System.out.println("Bye!");

// Force finalizers to be called:

System.gc();

}

}
I use j2sdk1.4 to compile it,then i type java Frog and get the results as shown below:
Not finalizing bases

Creating Characteristic is alive

LivingCreature()

Creating Characteristic has heart compile


Animal()

Creating Characteristic can live in water

Amphibian()

Frog()

Bye!

Frog finalize

But if i add the code System.out.println(DoBaseFinalization.flag); at the place of //******** in the main() method ,then the results will be like this:
Not finalizing bases

Creating Characteristic is alive

LivingCreature()

Creating Characteristic has heart

Animal()

Creating Characteristic can live in water

Amphibian()

Frog()

Bye!

Frog finalize

finalizing Characteristic is alive

finalizing Characteristic has heart

finalizing Characteristic can live in water

I really don't know why and want somebody to give to a hand,thanks a lot!


Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: the great puzzle of finalizing Posted: May 13, 2002 10:55 PM
Reply to this message Reply
I think the main point about finalizers is that they are practically useless. I think Bruce even suggested that the only marginal use that might squeezed out of them is a kind of debug-check thing where they only verify that an object that should have released resources (via a close() method, or something similar) has done so before the finalizer is hit; however, even this has the caveate that there is no guarantee that the finalizer will be called. This is true even if you use System.gc(). System.gc() does not force finalizers to be called -- rather it suggests to the JVM that now would be a good time to do it, if the JVM is so inclined[/b]. In other words, for spot-checking purposes if the finalizer is called and the resource hasn't been released, then you can raise a flag, which may help in improving the quality of the code. However, since there is no guarantee that the finalizer will ever be called, you definitely do not want to use it for resource freeing or anything else important.

What this really all boils down to is the simple fact that finalizers are not important.

Flat View: This topic has 1 reply on 1 page
Topic: mix languages in Java Previous Topic   Next Topic Topic: oh the java probrlems......help me !!!! multiple problems

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use