• 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
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

Explicit assigning of null values?

 
Ranch Hand
Posts: 697
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello Ranchers,
I got a doubt regarding the object creation and garbage collection.
Actually, suppose if we are creating many objects say String or Vector or other objects in a method, do we need to explicitly make all the references point to null?
public void method() {
String s1 = new String();
String s2 = new String();
String s3 = new String();
}
Here in the question, do we need to explicity make s1, s2, s3 null, before returning from this method? I am not sure about it, please guide me.
Appreciate your help, thanks.
 
Ranch Hand
Posts: 319
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Satish.
No you don't need to do that.
The moment the method exits, all local variables of that method become eligible for garbage collection. Unless you explicitly passed a reference of a local object to another scope from within that method. But that's not what you are doing. And even if you then set the reference object to null, the object itself will still not be eligible for garbage collection because it is referenced from somewhere else.
J
 
Satish Avadhanam
Ranch Hand
Posts: 697
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Jacques.
That was a great relief to me. Thanks.
 
reply
    Bookmark Topic Watch Topic
  • New Topic