Win a copy of The Way of the Web Tester: A Beginner's Guide to Automating Tests this week in the Testing forum!

# Question about GC and Objects

Christian Joseph
Ranch Hand
Posts: 43
i stumbled about this Test in SCJP by Kat and bertes . ..

on page 274

Which two are true about the objects created within main(), and eligible for garbage collection when line 14 is reached?

the answer is FIVE objects are created and two object are eligible for GC...

can someone help me

Ranch Hand
Posts: 64

if you face this kind of question you need to draw diagram to know which reference is point to which object. which one is eligible to GC.

Christian Joseph
Ranch Hand
Posts: 43
i get it now, Thanks! Ms. Naveen Madarapu...

so whenever i say "new Dozens" it will crreate a Dozen Object, PLUS a instance member like the int in the example

then since "d" reference object "NEW DOZENS()" has pointing two f***n thing the "d" and the "d[1]", which is become null, the line broke and then the DOzen of d and the int object, where left on the blackhole

thanks ! did i get it?

Ogeh Ikem
Ranch Hand
Posts: 180
The statement Dozens[] da = new Dozens[3] creates the first object i.e. a Dozens array (1). The statement da[0] = new Dozens() creates 2 objects i.e. a Dozens object (2) and an int array (3). The statement Dozens d = new Dozens() creates 2 objects i.e. a Dozens object (4) and an int array (5). Total objects created is 5.

The statements d = null and da[1] = null make objects (4) and (5) eligible for GC at line 14. Both statements are necessary or else objects (4) and (5) wont be eligible for GC at line 14.

Christian Joseph
Ranch Hand
Posts: 43
Ogeh Ikem wrote:The statement Dozens[] da = new Dozens[3] creates the first object i.e. a Dozens array (1). The statement da[0] = new Dozens() creates 2 objects i.e. a Dozens object (2) and an int array (3). The statement Dozens d = new Dozens() creates 2 objects i.e. a Dozens object (4) and an int array (5). Total objects created is 5.

The statements d = null and da[1] = null make objects (4) and (5) eligible for GC at line 14. Both statements are necessary or else objects (4) and (5) wont be eligible for GC at line 14.

THANKS!!! IT REALLY help me alot hearing from you guys!!