title: Initialize an array of a generic type in the constructor of a class without having to pass the Class<T> parameter
body: <p>I have the class:</p>
  
  <pre><code>class SomeClass&lt;T extends SomeInterface&gt;{
  
     private T[] myArray;
  
     public SomeClass()
     {
         // I want to initialize myArray in here to a default size of 100
         myArray = new T[100];  // this gives an error
     }
  
  }
  </code></pre>
  
  <p>I know I can fix that by requiring a parameter in the constructor as:</p>
  
  <pre><code>class SomeClass&lt;T extends SomeInterface&gt;{
  
     private T[] myArray;
  
     public SomeClass(Class&lt;T&gt; clazz)
     {       
         myArray= (T[]) Array.newInstance(clazz, 100);
     }
  
  }
  </code></pre>
  
  <h2>but it makes no scene having to pass the generic parameter twice.</h2>
  
  <p>in other words in order to instantiate an object from the class <code>SomeClass</code> I will have to do something like:</p>
  
  <pre><code>SomeClass&lt;SomeOtherClass&gt; obj = 
        new SomeClass&lt;SomeOtherClass&gt;(SomeOtherClass.class);
  </code></pre>
  
  <p>I program in c# and Java does not seem to be friendly. I don't even understand why it is not possible to cast <code>Object[]</code> array to <code>SomeOtherClass[]</code> array. In c# that will be possible...</p>
  
  <p>so my question is how can I avoid having to pass the <code>SomeOtherClass.class</code> parameter in order to be able to construct an array of the generic type in the constructor of the class...</p>