• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Knute Snortum
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Ganesh Patekar
  • Stephan van Hulst
  • Pete Letkeman
  • Carey Brown
Bartenders:
  • Tim Holloway
  • Ron McLeod
  • Vijitha Kumara

Explicit assigning of null values?  RSS feed

 
Ranch Hand
Posts: 697
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • Quote
  • Report post to moderator
Thanks Jacques.
That was a great relief to me. Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!