title: Creating an Object inside a loop
body: <p>Is it a good practice to create an Object inside a loop. I am pointing towards the following code:</p>
  
  <pre><code>for(some condition){
      SomeClass a = new SomeClass ();
      System.out.println(a);
  }
  </code></pre>
  
  <p>So this would create a new instance of SomeClass for each iteration. So number of instances will be equal to number of iterations. And then these will later be collected by GC.</p>
  
  <p>Is it better to reuse a SomeClass object inside the loop. Something like this:</p>
  
  <pre><code>SomeClass a = null;
  
  for(some condition) {
      a = new SomeClass();
      System.out.println(a);
  }
  </code></pre>
  
  <p>As far as I understand this, the second way is better as this will just create the SomeClass object once and will reuse it in every iteration. But I am doubtful. Please confirm this, or let me know where my fundamentals are incorrect.</p>