title: Type of template if the template is the return value (Java)
body: <p>I was wondering what is the data type of the template variable if the return is set to a template. I have seen this in a code somewhere but I do not know where does it cast the value retrieved from the session.</p>
  
  <pre><code>public class RequestObject {
      public &lt;T&gt; T getFromSessionMap(String sessionKey) {
          return (T)session.getAttribute(sessionKey);
      }
  }
  </code></pre>
  
  <p>The code for this outside is:</p>
  
  <pre><code>MyClassType type = request.getFromSessionMap("abc");
  </code></pre>
  
  <p>The line encounters ClassCastException when casting to my object. But when I add to watch session.getAttribute("abc"), it shows that the type is MyClassType. Any help would be appreciated.</p>
  
  <p>Apparently this special code for using template makes the return of getFromSessionMap a variable type and hence no need for a cast. This works on all cases but suddenly it failed in one part of the code.</p>