jQuery in Action, 3rd edition
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Question in Garbage collection Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Question in Garbage collection" Watch "Question in Garbage collection" New topic

Question in Garbage collection

Mathangi Shankar
Ranch Hand

Joined: Nov 01, 2004
Posts: 56
Please explain me the output of the following snippet(if possible with a diagram).

class I {
private I other;
public void other(I i) {other = i;}
class J {
private void m1() {
I i1 = new I(), i2 = new I();
I i3 = new I(), i4 = new I();
i1.other(i3); i2.other(i1);
i3.other(i2); i4.other(i4);
public static void main (String[] args) {
new J().m1();

Which object is not eligible for garbage collection after method m1 returns?

a. i1
b. i2
c. i3
d. i4
e. Compile-time error
f. Run-time error
g. None of the above

The answer is None of the above.

--------------------------------<br />SCJP1.4
Premkumar Gopal

Joined: Jun 12, 2004
Posts: 22
Since all the objects are created within the heap(in this case , the other happens to be an object of type I, the answer "None of the above is correct".

SCJP(1.4), OCP(8i) DBA, Websphere Certified Associate Developer, Websphere Certified Solution Developer, Websphere Portal Application Developer
Premkumar Gopal

Joined: Jun 12, 2004
Posts: 22
You can refer to KBs book (pp 435 - Section "Isolating a Reference" ) talks in depth

I agree. Here's the link: http://aspose.com/file-tools
subject: Question in Garbage collection
It's not a secret anymore!