jQuery in Action, 3rd edition
The moose likes Java in General and the fly likes static method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of REST with Spring (video course) this week in the Spring forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "static method" Watch "static method" New topic

static method

RJ Joshi

Joined: Dec 30, 2002
Posts: 12
Will an object which has been instantiated in a static method not be removed by garbage collector ?
Cindy Glass
"The Hood"

Joined: Sep 29, 2000
Posts: 8521
If the static method instantiates an object and the only reference to it is a local variable in the static method, then when the method is over, the object will be available for the gc().
If the static method instantiates an object and the reference to that object is a field outside the method (not a local variable) or is returned out of the static method, then there will be a reference to that object after the static method is complete, and the object will not be available for the gc() until THAT reference is gone also.

"JavaRanch, where the deer and the Certified play" - David O'Meara
Jasper Vader
Ranch Hand

Joined: Jan 10, 2003
Posts: 284
if the static method creates an object but the variable used to reference that object is outside the method, then that variable would have to be static, and would not be available until the entire class has gone away?
ps - i dont know how relevant that situation would e to real life, ie, if it would come up at all.

giddee up
wood burning stoves
subject: static method
It's not a secret anymore!