java looping - declaration of a Class outside / inside the loop

Posted by lisak on Stack Overflow See other posts from Stack Overflow or by lisak
Published on 2010-05-25T00:45:36Z Indexed on 2010/05/25 0:51 UTC
Read the original article Hit count: 224

Filed under:
|

when looping, for instance:

for ( int j = 0; j < 1000; j++) {}; and I need to instantiate 1000 objects, how does it differ when I declare the object inside the loop from declaring it outside the loop ??

 for ( int j = 0; j < 1000; j++) {Object obj; obj =}   

vs

Object obj; 
 for ( int j = 0; j < 1000; j++) {obj =}

It's obvious that the object is accessible either only from the loop scope or from the scope that is surrounding it. But I don't understand the performance question, garbage collection etc.

What is the best practice ? Thank you

© Stack Overflow or respective owner

Related posts about java

Related posts about loops