0

How can i instanciate the only constructor that my classes have and how can i parse using reflection api? //btw the real class uses Enum to get most of the data.

import java.lang.reflect.*;
import java.util.*;
import javax.swing.table.*;

public class Reflec{
    String[][] param={ {"John", "48"}, ... };//info read from file
    Class[] cl={ Person.class ,... };//My classes I want to instantiate
    paramClass[] pcl={{String.class, Integer.class}, ...}; 

    public Reflec(int i){ //After constructing I know all I need to instantiate
        this.i=i; //So now I know my info as cl[i], paramClass[i], and the param[i]
    }
//how can I Instance of the class in cl[i] with the params on param[i] previusly parsed as
//paramClass requires for integers.
}

Thanks, but the parse is still missing:

Constructor<?> ctor =cl[i].getConstructor( paramClass[i] ); //excellent
//I am still struggling to parse the Array to Object array  
//parsedArray = new Object={"John", 48}; not shure if that works and interpret 48 as Integer.
//Object object = ctor.newInstance( parsedArray );
corlaez
  • 1,352
  • 1
  • 15
  • 30
  • 1
    Is [`newInstance`](http://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#newInstance--) what you're looking for? Or if you want a constructor with arguments, use [`getDeclaredConstructor`](http://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getDeclaredConstructor-java.lang.Class...-) and then the `Constructor`'s `newInstance` method. – ajb Aug 15 '14 at 19:22
  • 1
    From the duplicate Q/A: Note that you don't need to do `Class> clazz = Class.forName(className);` because you already have the `Class> clazz` object. – Luiggi Mendoza Aug 15 '14 at 19:22

0 Answers0