• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Garbage Collection

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
public class XYZ{
ABC obj=new ABC();
mth1{
System.out.println(obj);
}

}
public class C{
public static void main{
XYZ obj1= new XYZ();
obj1.mth1();
}
}

in Class C when main method ends the object for XYZ is eligible for collection by the GC. But happens to the reference 'obj' created in XYZ..??? Is IT too eligible for collection..??
 
Ranch Hand
Posts: 1252
Spring Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Akash Bhatt:
public class XYZ{
ABC obj=new ABC();
mth1{
System.out.println(obj);
}

}
public class C{
public static void main{
XYZ obj1= new XYZ();
obj1.mth1();
}
}

in Class C when main method ends the object for XYZ is eligible for collection by the GC. But happens to the reference 'obj' created in XYZ..??? Is IT too eligible for collection..??



If main threads ends up then all objects are eligible for Garbage Collection.

It's quite obvious.

Pls correct me If I am wrong.
 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, the object of type ABC referenced by obj is eligible for garbage collection. You effectively have two linked objects with no active thread referencing either of them. Imagine a red kite tied to a blue kite. If you are holding the string to the blue kite and you let go, both kites will be blown away.
 
reply
    Bookmark Topic Watch Topic
  • New Topic