• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Explicit assigning of null values?

 
Satish Avadhanam
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.
 
Jacques Bosch
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