title: Static polymorphism with generics in Java
body: <p>I was hoping to create some clean code that can recursively dig into a collection and print the first Integer it finds. Coming from a C++ background that approach looked like this:</p>
  
  <pre><code>class Test {
  static void print(Integer i) {
    System.out.println(i);
  }
  
  static &lt;T&gt; void print(ArrayList&lt;T&gt; arr) {
    T entry= arr.at(0);
    Test.print (entry);
  }
  
  static void Test() {
      ArrayList&lt;ArrayList&lt;Integer&gt;&gt; arrayOfArrayOfInt= create();
      print( arrayOfArrayOfInt );
  }
  }
  </code></pre>
  
  <p>Unfortunately this doesn't work.</p>
  
  <p>One alternative is to give up on static polymorphism and create a function print(Object o) and then do a number of instanceof checks to branch to the right behavior. Is that the only way it can be done due to Java's type erasure or is there a more elegant approach?</p>