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 );
2
  • 1
    Is newInstance what you're looking for? Or if you want a constructor with arguments, use getDeclaredConstructor and then the Constructor's newInstance method. Commented Aug 15, 2014 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. Commented Aug 15, 2014 at 19:22

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.