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

# GC problem

vineet walia
Ranch Hand
Posts: 138

how many object are eligible for GC???

Balraj Momi
Ranch Hand
Posts: 45
I think four objects are eligible for GC.

Anastasia Sirotenko
Ranch Hand
Posts: 64
3 objects:

Balraj Momi
Ranch Hand
Posts: 45
Anastasia is right, there are 3 objects eligible.
It will be 4 If gb1 is assigned to null.

Nitish Bangera
Ranch Hand
Posts: 537
Vineet walia says
how many object are eligible for GC???

I guess you should draw a diagram and first give your answer so that you can verify with the ranchers.

Himanshu Kansal
Ranch Hand
Posts: 257
Number of objects actually created - Number of objects still having references assigned

Ankit Garg
Sheriff
Posts: 9543
33
vineet please Quote Your Sources when you post a question. You've posted multiple questions without a proper source. Your questions will be deleted if you don't give the source properly...

Nitish Bangera
Ranch Hand
Posts: 537
This is a famous examlab question....

Ankit Garg
Sheriff
Posts: 9543
33
Nitish Bangera wrote:This is a famous examlab question....

Its good that it is from a legal mock exam, but generally we expect the topic creator himself/herself to specify the source. That way its easier for everyone. Anyways its good that you are making efforts

S Ali
Ranch Hand
Posts: 129
3 objects ,right?

Ankit Garg
Sheriff
Posts: 9543
33
Its easy to find the number of object eligible for GC using a diagram

At line 7, the memory map looks like

At line 8 it becomes

at line 9 it becomes

and at line 10 it is

So 3 objects are eligible for GC (The one with the capital G in them)...

vineet walia
Ranch Hand
Posts: 138
Anastasia Sirotenko wrote:3 objects:

bro not geting your handy notation can you exp lain little bit??

vineet walia
Ranch Hand
Posts: 138
Ankit Garg wrote:Its easy to find the number of object eligible for GC using a diagram

thank Ankit Garg i thnk i will help me out

[Edit AG: Removed long quotes which were looking messy]

Ankit Garg
Sheriff
Posts: 9543
33
@vineet, you can use the @<NAME> to direct your message to someone specific instead of quoting whole posts. Like what you said in the last two posts can be done like -

@Anastasia, bro not geting your handy notation can you explain little bit??

@Ankit, thanks I thnk I will help me out

That way its easier to read as quoting long posts can look very messy...

Tanya Shetty
Ranch Hand
Posts: 40
Can somebody tell me why this line No. 7

does not give a null pointer exception, since, ultimately it does try to cast a null type to GARB type in the constructor..

I have tries this code and it runs fine!

Also, if i replace line no. 7 by,

I DO get the null pointer exception , why is this ???

Anastasia Sirotenko
Ranch Hand
Posts: 64
NullPointer comes from line 8, where you try to instantiate gb1.g.g , tho gb1.g is null after line 7

Ankit Garg
Sheriff
Posts: 9543
33
Tanya this is from the JLS

The null reference can always be cast to any reference type.

Himanshu Kansal
Ranch Hand
Posts: 257
Just check where exatly you get the exception, and you shall know

Fritz Guerilus
Ranch Hand
Posts: 65
Ankit that was a GREAT diagram.
Absolutely wonderful. I wonder how long it took you to draw it out.

The first time I answered this question I drew the diagram but I still got the answer wrong. I drew an extra object becuase I was confused.

Then I reviewed/drew the diagram again before looking in the forums for more explaination.

When I reviewed it for the 2nd time, I got the answer right. and double checked the forum.

The trick with all of these GC questions is that the class always HAS-A instance variable of some object type, and that can be confusing.

The best thing you can do is keep practicing and draw it out.