File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes Question about object creation Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Question about object creation" Watch "Question about object creation" New topic
Author

Question about object creation

Marlon Churchill
Greenhorn

Joined: May 08, 2011
Posts: 12
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.


Given

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?

As you can see, I'm a little confused.

Let the Washing Machine Tribe conquer all!
Joseph Kampf
Greenhorn

Joined: Mar 04, 2004
Posts: 26
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.


Given

When line 16 is reached, how many objects are available for garbage collection?


There are 2 wrinkles here.

First there is a static member of the class Alpha. So once you assign an object to Alpha.b1 you have an open reference to that object. That object will never be GCed until Alpha.b1 is set to null or another instance of Beta. So since b1 is assigned to Alpha.b1, b1 will not be GCed.

Also you don't set a2 to null, so a2.b2 is still around. So b2 is not eligible for GC.

So b1 can't be eligible, a2 and b2 are not eligible for GC. You created 4 objects, so only 1 object is eligible for GC. (That is a1)

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.


You created 4 objects. Objects are only created when "new" is used. You used new 4 times, so you have 4 objects created.

still, setting b1 and b2 both equal to null, seems to make them eligible for GC.

b1 was assigned to Alpha.b1 and will not be eligible for GC until the value of Alpha.b1 is changed.


Does setting a2.b2 = b2 mean the code can still reach b2, so b2 (although null) is not eligible for GC?

a2.b2 does mean that you can still reach b2. So setting b2 to null will not make it eligible for GC.

BTW, I would never assign Alpha.b1 us using the static member of a instance like a1.b1. Most IDEs will point this out as a potential source of a bug.
Ronald Castillo
Ranch Hand

Joined: Apr 16, 2011
Posts: 47

Hi Marlon,

This one could be pretty tricky for beginners like us. The tricky part is that class Alpha has 2 references which have the same name as other variables we created.

How many Objects are created? Only four.
Easy way to figure it out is putting a constructor on each class that prints a message when an instance is created (new is only used 4 times).
At the end of the program you'll see 4 instances were created. You may think that it may be 6 or 7 since class Alpha has 2 reference variables to a Beta object, BUT, these are never INITIZALIZED hence only 4 objects were created.


Regarding how many objects are eligible for GC, only 1 is, which is Alpha a1. The other objects that were set to null are linked to other reference variables and that's why they're not eligible for GC.
Try getting a blank sheet, drawing 4 objects and 4 reference variables. Two of those objects, the Alpha objects hold reference variables themselves, so draw them too, only difference is that a static b1 reference is shared between classes (I'm guessing you know what the static keyword means).
That may help a little understanding this problem. That's how I figured it out since I was also getting confused by the reference variables inside the Alpha class.
Try it, if it didn't help much let me know.
Marlon Churchill
Greenhorn

Joined: May 08, 2011
Posts: 12
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[0]= new Dozens();

is the Dozens object in da[0] created. A int array da[0].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.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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[0]= new Dozens();

is the Dozens object in da[0] created. A int array da[0].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.


Hi Marlon

Remember the following always:

a) An instance member will be present for each object of a class type that is instantiated. If I rephrase, I could say that whenever an instance of a class is created i.e. whenever I use new keyword, it will have its own copy of instance member. Hence, when we say, , the new Dozens object that will be instantiated and stored in the Dozens array will also have an array copy of . Similarly, when you say , another instance of Dozens is created and stored along with its copy of . So how many objects do you have now? Two Dozens objects and their two array instance members on heap. And one is the Dozens [] array object. That makes it 5. Hope this makes the fundamental concept clear.


~ Mansukh
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Question about object creation
 
Similar Threads
Doubt regarding GC
Garbage Collection problem-2
Objects Eligible for GC
Garbage Collection Clarification
K&B SCJP6 Chapter 3- Q11 a GC question