Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes GC question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "GC question" Watch "GC question" New topic

GC question

howie jao

Joined: Nov 08, 2009
Posts: 14
class Dozens{
int[] dz = {1,2,3,4,5,6,7,8,9,10,11,12};
public class Eggs{
public static void main(String[] args){
Dozens [] da = new Dozens[3];
da[0] = new Dozens();
Dozens d = new Dozens();
da[1] = d;
d = null;
da[1] = null;

In above code, Five object were created and two object are eligible for GC.
Dozens [] da = new Dozens[3] < -- create one object: da array
da[0] = new Dozens() <--- this line create two object: Dozens and dz array
Dozens d = new Dozens() <-- this line create two object: Dozens and dz array
d = null <-- Dozens object and its dz array are eligible for GC.

Is my intepretation correct for number of object created and object for GC? I just want to make sure that I am understanding the code correctly/

Question 2:
class Beta{}
class Apha{
static Beta b1;
Beta b2;
public class Tester{
public static void main(String[] args){
Beta b1 = new Beta(); Beta b2 = new Beta();
Alpha a1 = new Alpha(); Alpha a2 = new Alpha();
a1.b1 = b1;
a1.b2 = b1;
a2.b2 = b2;
a1 = null; b1 = null; b2=null

only one object is eligible for GC , why?

Thanks for any help
Ankit Garg

Joined: Aug 03, 2008
Posts: 9291

howie please Quote Your Sources when you post a mock exam question...

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
howie jao

Joined: Nov 08, 2009
Posts: 14
Hi, sorry
it is from Sierra and Bates book
Henry Wong

Joined: Sep 28, 2004
Posts: 18534

Please search the ranch first -- those two questions have been asked many times before (note: the search link is near the top of this page).


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
I agree. Here's the link:
subject: GC question
Similar Threads
Question about object creation
Problem with GC exercises
Garbage collection program really confusing me?
Please Explain Q.10 And Q.11 of Chapter 3 from K&B SCJP6
Garbage Collection problem-2