title: Non-generic reference to generic class results in non-generic return types
body: <p>I have a legacy class that the class itself is not a generic but one of its methods return type uses generics:</p>
  
  <pre><code>public class Thing {
      public Collection&lt;String&gt; getStuff() { ... }
  }
  </code></pre>
  
  <p><code>getStuff()</code> uses generics to return a collection of strings. Therefore I can iterate over <code>getStuff()</code> and there's no need to cast the elements to a <code>String</code>:</p>
  
  <pre><code>Thing t = new Thing();
  for (String s: t.getStuff())  // valid
  { ... }
  </code></pre>
  
  <p>However, if I change <code>Thing</code> itself to be a generic but keep everything else the same:</p>
  
  <pre><code>public class Thing&lt;T&gt; {
      public Collection&lt;String&gt; getStuff() { ... }
  }
  </code></pre>
  
  <p>and then keep using the non-generic reference to <code>Thing</code>, <code>getStuff()</code> no longer returns <code>Collection&lt;String&gt;</code> and instead returns a non-typed <code>Collection</code>. Thus the client code does not compile:</p>
  
  <pre><code>Thing t = new Thing();
  for (String s: t.getStuff())  // compiler complains that Object can't be cast to String
  { ... }
  </code></pre>
  
  <p>Why is this? What are the workarounds?</p>
  
  <p>My guess is that by using a non-generic reference to a generic class, Java turns off all generics for the entire class. This is pain, because now I've broken my client code by making Thing a generic.</p>
  
  <p><strong>Edit:</strong> I'm making Thing generic for another method which is not listed in the above example code. My question is educational as to why the above cannot be done.</p>