Tarun Mohandas wrote:So is it safe to say that the object created at line 7 is available for GC after line 9 since local variable o is referenced to null and the object created at line 7 is dereferenced?
~ Mansukh
Mansukhdeep Thind wrote:Tarun
I would suggest you take a piece of paper and make figures of objects and their respective references. It makes the picture much more clear. I also used to get confused in the beginning when dealing with such questions. Always better to draw the figures and then go about answering. This is a simple scenario of calculating number of objects available for GC. The waters get murkier when String literals and objects come into picture.
~ Mansukh
The sooner you get used to using pencil and paper the better; that was a good suggestion. But you do not need to know which variable lives on the heap and which on the stack to be able to answer such questions correctly.Tarun Mohandas wrote: . . . The trick is to just separate local variables (in stack) to objects and instance variables (in heap).
Campbell Ritchie wrote:The sooner you get used to using pencil and paper the better; that was a good suggestion. But you do not need to know which variable lives on the heap and which on the stack to be able to answer such questions correctly.
Tarun Mohandas wrote:I came across this question when I was simply browsing through some questions online.