java - Method Created Objects Deleted on Exit -
this question has answer here:
- garbage collection on local variable 3 answers
are objects / fields created in method deleted upon exit of specific method?
example:
public static void createfolder() { file folder = new file(c:\example\path "foldername"); folder.mkdir(); }
would memory used store file "folder" deleted upon exit of "createfolder" method?
the file
object referred folder
becomes eligible garbage collection upon exit method since reference (folder
) goes out of scope @ point.
when garbage collected impossible tell exactly, sometime after point.
more generally, local variables in method allocated on stack , deleted when go out of scope. if primitives (int, float, long, etc) cease exist immediately. if references (as in case) reference ceases exist object referred continues exist long reference exists. example, if createfolder()
method return value of folder
, , caller preserved reference in 1 of own variables, file
object not (yet) eligible gc. when all references object cease exist object eligible gc. language not specify when gc occurs.
this complicated little esoteric topics having "weak" references can ignore time being.
Comments
Post a Comment