mardi 16 janvier 2018

java: ERROR: "cannot find symbol – class Constructor" when making instance from String, with parameters

I searched through all the questions here that involve 'reflection' to instantiate a parameterised object using a String of the class name, but I cannot get it to work because Java doesn't recognise Constructor as a class.

Class myClass = Class.forName("MyClass");

Class[] types = {Double.TYPE, String.TYPE};
Constructor constructor = myClass.getConstructor(types);

Object[] parameters = {1.0, "hello"};
Object instanceOfMyClass = constructor.newInstance(parameters);

This is the code I tried using, I also tried writing the parameters differently, but those are irrelevant to the problem (please reply to the Constructor class issue). How do I make this work?





Aucun commentaire:

Enregistrer un commentaire