Claire Yang

Ranch Hand
+ Follow
since Aug 30, 2002
Merit badge: grant badges
For More
Cows and Likes
Cows
Total received
In last 30 days
0
Forums and Threads

Recent posts by Claire Yang

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 ]
20 years ago
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.
20 years ago
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!)
Thanks a lot, dennis.

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?
In the following code, after line "doSomething", there are 2 objects of class A and class C eligible for GC, right?
Maulin,

I see what you mean, I forgot that I created 2 objects in the main method, thanks a lot.
Stephen,
Value of the field depends on the declared type of the variable, since a is declared as Animal, a.size=42.
In the following code, why super.i & super.s changed in method compare()?
Hi, Breeze,
Here is the article that helps a lot:
maha's solution
[ June 05, 2003: Message edited by: Claire Yang ]
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.
21 years ago
Valentin, thanks a lot.
Valentin, thanks a lot for always replying quickly and patiently.