title: How to use varargs as parameters of newInstance() to call a constructor and create an instance?
body: <p>I'm an autodidact and I'm trying to do this homework, but I didn't find any solution yet.</p>
  
  <p>I don't know how to use varargs.I can't pass them to the newInstance() method to invoke the constructor that I need.
  But the main problem is that I would like to write them from the command line using "args" but I don't know how to do.</p>
  
  <p>Please any advice? Thanks a lot!</p>
  
  <pre><code>import java.lang.reflect.*;
  import java.io.*;
  
  public class VarargsTest{
      public static void main(String[] args)throws IOException{
          Class theClass = null;
          BufferedReader br = null;
          br = new BufferedReader(new InputStreamReader(System.in));
          try{
              theClass = Class.forName(args[0]); // what class I want to use ?
          }
          catch(ClassNotFoundException e){
              e.printStackTrace();
              System.exit(1);
          }
          System.out.println(theClass);
          Object obj = null;
          System.out.print("number of constructor's parameters :"); 
          String buf = null;
          try{
              buf = br.readLine(); //Waiting the number of parameters for the constructor
          }
          catch(IOException ioe){
              ioe.printStackTrace();
          }
          Constructor[] constructors = theClass.getConstructors(); // constructor of that class
          for(Constructor constructor : constructors) {
              if(constructor.getParameterCount() == Integer.parseInt(buf)) {//checking that the number of parameters are matching
                  if(constructor.getParameterTypes()[0] == VarargsC.class){// IL PRIMO PARAMETRO DEL COSTRUTTORE DEVE ESSERE DEL TIPO DELLA CLASSE
  
                      try{
  
                          obj = constructor.newInstance(new Object[]{"first", "second"," more"}); // trying some elements, but I would like use args[1]
                      }
                      catch(InstantiationException | IllegalAccessException | InvocationTargetException e) {
                          e.printStackTrace();
                          System.exit(1);
                      }
                  }
              }
          }
          System.out.println();
          System.out.println("There isn't a constructor with " + buf + " parameters");
          System.out.println();
          if(obj != null) {
              System.out.println("I have instantiate an object!");
              System.out.println(obj);
          }else{
              System.out.println("I'm not able to instantiate an object'");
          }
      }
  }
  
  class VarargsC{
      String str1;
      String str2;
      String str3;
          public VarargsC(String str1, String str2, String str3){
              this.str1 = str1;   
              this.str2 = str2;
              this.str3 = str3;
          }
          public String toString(){
              return "VarargsC{" +
                      "str1=" + str1 +"\n" +
                      "str2=" + str2 +"\n" +
                      "str3=" + str3 + "\n" + '\'' +
                      '}';
          }
  }
  
  
  /*class VarargsC{
  
      String[] data;
          public VarargsC(String... str){
              data = new String[5];
              for(int i= 0; i&lt; str.length; i++)
                                  for(String a: str)
                  this.data[i] = a;
          }
  
          public void toString(String... str){
              for(String a: str)
                  System.out.println(a + "\n");
          }
  }*/
  
  </code></pre>