File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes new object in method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "new object in method" Watch "new object in method" New topic

new object in method

Chris Palau

Joined: Jan 21, 2009
Posts: 4

I am reading the SCJP 6.0 Certification book from Kerry and Kathy and I have a doubt I can't clarify with the book info. I googled but I didn't found any info about that. My question is about the JVM garbage collector.

For example, if I have a method like this:

public void getData() {

StringBuilder sb = new StringBuilder();


The book explains this: "local variables are in the stack and instance variables / objects in the heap. When a method finalizes and returns: local variables are eligible for garbage collection", but my question is: what about the object instantiated in the method ? Is automatically eligible for garbage collection also ?

if I have the same example but in a for statement like this:

for (int i=0; i <1000; i++) {
StringBuilder sb = new StringBuilder();

StringBuilder object is elegible for garbage collection when the for loop finish ?

Omar Al Kababji
Ranch Hand

Joined: Jan 13, 2009
Posts: 357

Omar Al Kababji - Electrical & Computer Engineer
[SCJP - 90% - Story] [SCWCD - 94% - Story] [SCBCD - 80% - Story] | My Blog
Nitesh Kant

Joined: Feb 25, 2007
Posts: 1638

Not a performance question. Moving to SCJP certification forum.
Please CarefullyChooseOneForum while posting.

apigee, a better way to API!
Rajshekhar Paul
Ranch Hand

Joined: Oct 17, 2006
Posts: 140
Objects created with local variables are always eligible for garbage collection unless the reference to that object is passed outside the scope of that variable.

In the above case, the scope of s is the method body only. But as this method returns the object reference in a return statement, the object will not eligible for
GC after the execution comes out of the method body.

But in the above code, the object refernced by s will be eligible for GC once the execution comes out of the method body as this object is not referenced furthur.

Hope this helps.

When it's obvious that you have to do it, just do it without shattering your thoughts over different directions.
RaviNada Kiran
Ranch Hand

Joined: Jan 30, 2009
Posts: 528
Thank You Very Much Rajsekhar it is really valuable for me

If you want something you never had do something which you had never done
Abhi vijay
Ranch Hand

Joined: Sep 16, 2008
Posts: 509
I had seen a question in Devaks's Simulator. dont remember the number.

It was adding an object into a collection(Vector), so its not eleigible for GC. But what if this collection is declared inside a method?
Punit Singh
Ranch Hand

Joined: Oct 16, 2008
Posts: 952
then collection object itself is eligible for gc after the end of that method.

I agree. Here's the link:
subject: new object in method
It's not a secret anymore!