title: Java - Instance method for specific generic instances
body: <p>can I make a method that works on specific type of a generic class.</p>
  
  <p>For example, I have my class below:</p>
  
  <pre><code>package utils;
  
  public class MyArray&lt;T&gt; {
      // Constants
      private static final int INITIAL_SIZE = 10;
  
      // Instance Fields
      private T[] elms;
      private int size;
  
      // Constructor
      public MyArray () {
          elms = (T[]) new Object[INITIAL_SIZE];
          size = 0;
      }
  
      // Methods
      public void add (T elm) {
          if (elms.length == size)
              increaseSize();
  
          elms[size++] = elm;
      }
  
      @Override
      public String toString () {
          String str = "[" + elms[0];
  
          for (int i = 1; i &lt; size; i++)
              str += ", " + elms[i];
  
          str += "]";
  
          return str;
      }
  
      // Helper Methods
      private void increaseSize () {
          T[] temp = (T[]) new Object[elms.length];
          for (int i = 0; i &lt; temp.length; i++)
              temp[i] = elms[i];
  
          elms = (T[]) new Object[2 * temp.length];
  
          for (int i = 0; i &lt; temp.length; i++)
              elms[i] = temp[i];
      }
  }
  </code></pre>
  
  <p>Can I make a method that will work on <code>MyArray&lt;Integer&gt;</code> or <code>MyArray&lt;Long&gt;</code> and throws an exception on other types?</p>
  
  <p>I tried doing the following:</p>
  
  <pre><code>public T sumOfElms () {
      if (T instanceof Integer) {
          // code here
      }
  }
  </code></pre>
  
  <p>But it doesn't seem to work.</p>
  
  <p>Any ideas or thoughts on how I can do it?</p>