the flow of finalize()/in main() without any object how it will proceed
Jaydeep Singh
Ranch Hand
Joined: Oct 02, 2000
Posts: 43
posted
0
Hi Friends, I have two problems in understanding the following code, solved example of TIJ. Prblem 1> When we will pass the argument as "finalize"from the command line, it will check "if{--else --}" condition is true,then how the main() will proceed??? Problem 2> Hard time to understand the flow of the finalise() method in respect of composit class and inheritence. Pl. if someone can explain it, I will be more gratefull to her/him. thanksInAdvance Jaydeep
c07:Frog.java // Testing finalize with inheritance. 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() { System.out.println( "LivingCreature finalize"); // Call base-class version LAST! if(DoBaseFinalization.flag) try { super.finalize(); } catch(Throwable t) {} } } class Animal extends LivingCreature { Characteristic p = new Characteristic("has heart"); Animal() { System.out.println("Animal()"); } protected void finalize() { System.out.println("Animal finalize"); if(DoBaseFinalization.flag) try { super.finalize(); } catch(Throwable t) {} } } class Amphibian extends Animal { Characteristic p = new Characteristic("can live in water"); Amphibian() { System.out.println("Amphibian()"); } protected void finalize() { System.out.println("Amphibian finalize"); if(DoBaseFinalization.flag) try { super.finalize(); } catch(Throwable t) {} } } public class Frog extends Amphibian { Frog() { System.out.println("Frog()"); } protected void finalize() { System.out.println("Frog finalize"); if(DoBaseFinalization.flag) try { super.finalize(); } catch(Throwable t) {} } public static void main(String[] args) { if(args.length != 0 && args[0].equals("finalize")) 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(); } } ///:~
Jaydeep Singh
Ranch Hand
Joined: Oct 02, 2000
Posts: 43
posted
0
Hi, I'm just wondering, I did not get any response from anybody, What happened is it not up to standard or what??? With Regards jaydeep
Kathy Rogers
Ranch Hand
Joined: Aug 04, 2000
Posts: 103
posted
0
OK, I'll try and give you an explanation although I'm not quite sure what you need to know. That if statement checks that the argument length passed in when you run the program isn't 0 and that the first arguement is "finalize" (so running the program with the command "java Frog finalize" would make it true as would the command "java Frog finalize other random arguments"). If the if clause is true, the static boolean field,flag, of DoBaseFinalization is set to true. If it's not true (i.e. you run the program with the command "java Frog" or "java Frog something finalize" etc etc), then the Boolean flag is left at it's initialization value of false. There's a System.out.println which tells the user if the flag isn't set. That's all that if /else does. So let's look at the class hierarchy Frog extends Amphibian extends Animal extends LivingCreature So the next thing we do is we create a new frog, which in turn calls the constructor of Amphibian, calls constructor of Animal, calls constructor of LivingCreature - lots of printlns! But each class, apart from frog, contains the line Characteristic p = new Characteristic ("some String"). And each of these creates a new instance of Characteristic. And p is the label for that instance. Frog's inherited version of p ends up pointing at LivingCreature's Characteristic but all those other objects are floating around in memory without anything referencing them. And when Frog disappears, nothing will reference any of them. So when garbage collection happens, one of two things happen, depending on whether or not that static DoBaseFinalization.flag is set to true or false. There's a check in the finalize methods:- if (DoBaseFinalization.flag) If DoBaseFinalization.flag is set to false, then all that happens is Frog's version of finalize is called to clear up the new Frog(). And finalize is called for all those Characteristic objects that have been floating around and aren't referenced at all, now the Frog has gone. If it's true, the Frog finalize is called but Frog's finalize makes a call to finalize in the super class, Amphibian, which in turn calls finalize in the super class, Animal, which in turn calls the finalize in the super class, LivingCreature. And then there's all those unreferenced Characteristic objects to clean up too. So Characteristic's finalize method is called for them. And that's that. If you run the program now and look at all the lines displayed, hopefully it should make more sense. If you got any more specific questions, I'd be happy to try and answer them. Hope you're not more confused now! Kathy