title: Life time of local variable in Java
body: <pre><code>     public List&lt;Map&lt;String,class1&gt;&gt; methodName(String Parameter)
       {
            //create instance of List
            List&lt;Map&lt;Strig,class1&gt;&gt; list1 = new ArrayList&lt;Map&lt;String,class1&gt;&gt;();
  
            for(ArrayList arrayList : someGlobalList)
            {
                //create instance of class1
                //Initialize class1 with values in arrayList
                //Put this class1 as a value to tempMap which is created inside this loop
                //Put that tempMap into List
            }
         return list1;
       }  
  </code></pre>
  
  <p><strong>My doubt</strong></p>
  
  <p>I understand that we cannot guarantee garbage collection. But <a href="https://stackoverflow.com/questions/4557209/life-cycle-of-local-java-objects-created-during-a-method-call">this question</a> says that <code>when we return a local reference</code> that will not be garbage collected. In my case, class1 is a local Object. But I am returning <code>list1</code>. Is it safe to use those objects from the caller function as <code>list1.get("key")</code>. It will return the <code>class1</code> Object. Can I safely use the class members? </p>