title: Creating new instance from Class with constructor parameter
body: <p>I have situation where my Java class needs to create a ton of certain kind of objects. I would like to give the name of the class of the objects that are created as a parameter. In addition, I need to give the created class a parameter in its constructor. I have something like</p>
  
  <pre><code>class Compressor {
  
      Class ccos;
  
      public Compressor(Class ccos) {
          this.ccos = ccos;
      }
  
      public int getCompressedSize(byte[] array) {
          OutputStream os = new ByteArrayOutputStream();
          // the following doesn't work because ccos would need os as its constructor's parameter
          OutputStream cos = (OutputStream) ccos.newInstance();
          // ..
      }
  }
  </code></pre>
  
  <p>Do you have any ideas how I could remedy this?</p>
  
  <p>Edit:</p>
  
  <p>This is part of a research project where we need to evaluate the performance of multiple different compressors with multiple different inputs. <code>Class ccos</code> is a compressed <code>OutputStream</code> either from Java's standard library, Apache Compress Commons or lzma-java.</p>
  
  <p>Currently I have the following which appears to work fine. Other ideas are welcome.</p>
  
  <pre><code>OutputStream os = new ByteArrayOutputStream();
  OutputStream compressedOut = (OutputStream) ccos.getConstructor(OutputStream.class).newInstance(os);
  final InputStream sourceIn = new ByteArrayInputStream(array);
  </code></pre>