title: How to create a type safe generic array in java?
body: <p>I want to create a generic array in java maintaining the type safety usually offered by Java. </p>
  
  <p>I am using this code :  </p>
  
  <pre><code>class Stack&lt;T&gt; { 
  
  private T[] array = null;
  public Stack(Class&lt;T&gt; tClass, int size) {
     maximumSize = size;
     // type unsafe
     //array = (T[])new Object[maximumSize];
  
     this.array = (T[])java.lang.reflect.Array.newInstance(tClass,maximumSize);
  }
  </code></pre>
  
  <p>is this code type safe? ans if so, why? why if it is type safe I need a cast? </p>