title: Dead Store to local Variable in Java
body: <p>What exactly is the mechanism behind the bug- dead store to a local variable.. ?? </p>
  
  <p>Some say that dead store to variable bug occurs when you assign a value to variable but do not use that variable again in that code block/ function (I am facing the bug of same kind).</p>
  
  <pre><code>void foo(){
  String str = someObj.getString();
  //some code but str is never used after assignment...
  }
  </code></pre>
  
  <p>But there is also a case where you tend to initialize a variable with a value, but later in the subsequent instruction, you replace the value of the variable with some other value.</p>
  
  <pre><code>List&lt;String&gt; list = new ArrayList&lt;&gt;();
  list = someobj.getList();
  </code></pre>
  
  <p>In this case, object created by new ArrayList&lt;>() is never used which is also defined as dead store.</p>
  
  <p>We have a mechanism of garbage collection in java, which looks after such orphan objects and is responsible to optimize the memory.
  So if garbage collection takes care of memory leaks, then why do we get this bug of dead store to local variable?</p>
  
  <p>can garbage collector play any role in this bug?</p>
  
  <p>Its pretty clear that GC is not able to identify such dead stores!! But then, why can GC not able to identify it?? </p>
  
  <p>Note:I am of the opinion, developers should avoid such mistakes of assigning values which are never used further. But still this is one doubt that has come in my mind and now i want a well structured and a technical answer to it.. ;)  </p>