Thanks a lot, chi Lin. I changed the original code to "while(!((reply=br.readLine()).equals("n")))" and it works, but could you explain why? Does readLine() method create a String object(in heap) from the console input and return it? [ May 14, 2004: Message edited by: Claire Yang ]
I wrote the following code to read user's input repeatedly until user types "n", but whatever I typed, the while condition is always true, can anyone tell me what's the problem of the code? Thanks.
Thanks for your reply, Ray. I made a mistake to think that when the Frog object was created, it's parents were also created, actually were the contents of the constructors executed and there should be only 4 objects on the heap--1 Frog & 3 Characteristic.
Here is the "Inheritance and finalize( )" example of ch7 in the book "thinking in java--2e":
Output by typing "java Frog": 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 Output by typing "java Frog finalize": Creating Characteristic is alive LivingCreature() Creating Characteristic has heart Animal() Creating Characteristic can live in water Amphibian() Frog() bye! Frog finalize Amphibian finalize Animal finalize LivingCreature finalize finalizing Characteristic is alive finalizing Characteristic has heart finalizing Characteristic can live in water The book explains that-- "When you use composition to create a new class, you never worry about finalizing the member objects of that class. Each member is an independent object, and thus is garbage collected and finalized regardless of whether it happens to be a member of your class. With inheritance, however, you must override finalize( ) in the derived class if you have any special cleanup that must happen as part of garbage collection. When you override finalize( ) in an inherited class, it�s important to remember to call the base-class version of finalize( ), since otherwise the base-class finalization will not happen." Why "the base-class finalization will not happen" since Java API says "finalize()is called by the garbage collector on an object when garbage collection determines that there are no more references to the object"? (Sorry for the long post!)
In the above program, class Q's instance variable b hide class P's b and class Q inherits m() method from class P, how to explain that when object q calls m() method, it doesn't "see" its own b but class P's b?
I want to thank all the participants(askers & answerers) of JavaRanch Big Moose Saloon, especially Bert, Dan, Kathy and Valentin(alphabetically, ), thank you for making "Java life" eaiser for me.