• 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

GC for objects added to collections

 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Suppose I have code like this.

class A{}
class Test{
public static void main(String args[]){
A obj=new A();
Vector v=new Vector();
v.add(obj);
obj=null;
// rest of code here
}
}

The object referred to by obj will not get garbage collected until I say v=null. Am I right ?

Thanks
Devi
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Until you say v = null, or until the variable goes out of scope (i.e. at the end of this method).
 
Ranch Hand
Posts: 485
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hi
i didn't get this point

until the variable goes out of scope (i.e. at the end of this method

are u saying that locally created object will be eligible for the GC, once it gone out of scope like the LOCAL Variable.

clear my doubt.
 
Sreedevi Vinod
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, that is what he means. When a method returns, the locally created objects go out of scope, unless they are exported out of the method as a return value or an exception.

Thanks
Devi
 
reply
    Bookmark Topic Watch Topic
  • New Topic