Marlon Churchill wrote:This question is about how Java creates objects and when. I think I'm missing some basic concept. the code and question are from the Sierra and Bates SCJP 6 book, Chapter 3, question 11.
When line 16 is reached, how many objects are available for garbage collection?
The correct answer is 1, but I seem to count 3. b1, b2, and a1.
Backing up a bit, how many objects were created? I'm not really sure.
Is it seven? a1, a1.b1, a1.b2, b1, b1, a2, and a2.b2 ? Since Alpha.b1 is static, I think there is no separate a2.b1 object, the static Beta b1 belongs to the class Alpha.
or is it four? a1, b1, b2 and a2. The variables in the Alpha class are reference variables, not objects.
still, setting b1 and b2 both equal to null, seems to make them eligible for GC.
Does setting a2.b2 = b2 mean the code can still reach b2, so b2 (although null) is not eligible for GC?
Marlon Churchill wrote:Hi,
Sorry for the late reply. Thanks to both of you for your assistance.
@Joe - The questions on the SJCP are deliberately obfuscatory and use poor programming practices to enhance difficulty. Or so the Sierra and Bates SJCP books states.
@Ron - diagramming it out helped a lot. Unfortunately, I have another question about question 10 of the same chapter. Here is the code
The question asks which are true about ojbects created within main and eligible for garbage collection at line 14 (the comment line)
The correct answers are 5 objects created and two eligible for GC at line 14. I can figure out the GC part.
So we have a dozens array da. At first I thought it created a Dozens object in each element of the array when da is created, but I don't think it does. only at line
da= new Dozens();
is the Dozens object in da created. A int array da.dz (also an object) is also created. Is this correct?
then Dozens d= new Dozens (); creates another two objects. I think I get it.
Sorry for the questions. I worked my way through many of the sample problems in the Murach Java 6 book and thought I had a good handle on Java, but these questions are very detailed.
WHAT is your favorite color? Blue, no yellow, ahhhhhhh! Tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koophttps://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton