title: java generics compile error, generic class on the stack
body: <p>I'm not sure why this doesn't work in Java:</p>
  
  <pre><code>import java.util.Map;
  
  public class FreqCounter&lt;T,R&gt; {
      private Map&lt;T, Integer&gt; hist;
      private R item;
      public FreqCounter (final R item_) {
          item = item_;
      }
      public T getMostFrequentElement() {
          T most_frequent_element = T();
          Integer highestcount = 0;
          for(T t : item) {
              Integer count = hist.get(t);
              if(count == null) {
                  hist.put(t, 1);
              }
              else {
                  hist.put(t, count + 1);
              }
              if(count + 1 &gt; highestcount) {
                  most_frequent_element = t;
                  highestcount = count + 1;
              }
          }
          return most_frequent_element;
      }
  }
  
  
  class HelloWorld {
      public static void main(String[] args) {
          String s = "aaabbcccc";
          FreqCounter&lt;Character, Integer&gt; counter = new FreqCounter&lt;Character, Integer&gt;(s);
      }
  }
  </code></pre>
  
  <p>Problem lines:</p>
  
  <pre><code> 1. T most_frequent_element = T();
   2. for(T t : item)
   3. FreqCounter&lt;Character, Integer&gt; counter = new FreqCounter&lt;Character, Integer&gt;(s);
  </code></pre>
  
  <ol>
  <li>Cannot find symbol: method T()</li>
  <li>required: array or java.lang.Iterable, found: R</li>
  <li>Required java.lang.Integer
  Found: java.lang.String
  reason: actual argument java.lang.String cannot be converted to java.lang.Integer by method invocation conversion</li>
  </ol>
  
  <p>What I was trying to do was make a class that could count how many times an element in an iterable container shows up. Originally I just wanted to make it to count characters in a string but I thought I could make it more general. I think some of this would work in C++?</p>
  
  <p>Also, does <code>FreqCounter&lt;Character, Integer&gt; counter = new FreqCounter&lt;Character, Integer&gt;(s)</code>; need to be "newed" as opposed to declared on the stack?</p>