title: java - why do we sometimes need <E> in static method declarations?
body: <p>I get the following error "Cannot make a static reference to the non-static type E" for the method below:</p>
  
  <pre><code>public static void reverse(E[] a) {
         Stack&lt;E&gt; buffer = new Lab8&lt;&gt;(a.length); 
  
         for (int i=0; i &lt; a.length; i++)
            buffer.push(a[i]);
  
         for (int i=0; i &lt; a.length; i++)
            a[i] = buffer.pop();
  }
  </code></pre>
  
  <p>now I know that doing the following will fix the error, but I don't get the point of &lt; E > (why we need it in this case), and it doesn't make sense to define it in the method like that? I do get how to use it for instance in declaring generic classes and instances, but this is the first time I've seen it used this way...</p>
  
  <pre><code>public static &lt;E&gt; void reverse(E[] a) {
         Stack&lt;E&gt; buffer = new Lab8&lt;&gt;(a.length); 
  
         for (int i=0; i &lt; a.length; i++)
            buffer.push(a[i]);
  
         for (int i=0; i &lt; a.length; i++)
            a[i] = buffer.pop();
  }
  </code></pre>
  
  <p>This is the full code for reference:</p>
  
  <pre><code> package labs;
  
  import java.util.Arrays;
  
  public class Lab8&lt;E&gt; implements Stack&lt;E&gt; {
  
   public static final int CAPACITY=1000;
   private E[] data;
   private int t = -1;
   public Lab8() { this(CAPACITY); }
   public Lab8(int capacity) {
  // default array capacity
  // generic array used for storage
  // index of the top element in stack
  // constructs stack with default capacity // constructs stack with given capacity // safe cast; compiler may give warning
     data = (E[ ]) new Object[capacity];
  
   }
   public int size() { return (t + 1); }
   public boolean isEmpty() { return (t == -1); }
   public void push(E e) throws IllegalStateException {
        if (size() == data.length) throw new IllegalStateException("Stack is full");
        data[++t] = e;
   }
  
   public E top() {
       if (isEmpty()) return null;
          return data[t];
   }
  
   public E pop() {
       if (isEmpty( )) return null;
       E answer = data[t];
       data[t] = null;
       t--;
       return answer;
   }
  
   public static void reverse(E[] a) {
          Stack&lt;E&gt; buffer = new Lab8&lt;&gt;(a.length); 
  
          for (int i=0; i &lt; a.length; i++)
            buffer.push(a[i]);
  
          for (int i=0; i &lt; a.length; i++)
            a[i] = buffer.pop();
   }
  
   public static void main(String[] args) {
              Integer[] a = {4, 8, 15, 16, 23, 42}; // autoboxing allows this 
              String[] s = {"maryam", "ricardo", "Mostafa", "Ahmend", "Hitler"};
              System.out.println("a = " + Arrays.toString(a)); 
              System.out.println("s = " + Arrays.toString(s)); 
              System.out.println("Reversing...");
              reverse(a);
              reverse(s);
              System.out.println("a = " + Arrays.toString(a)); System.out.println("s = " + Arrays.toString(s));
   }
  }
  </code></pre>