Allora ci sono diverse cose da considerare, prima di tutto il nome della classe deve essere completo cioè ci vuole anche il package.
Seconda cosa devi sapere che costruttore questa classe ha.
codice:
public static void main(String[] args)
{
String className = "java.lang.String";
try
{
Class<?> clazz = Class.forName(className);
//SE L'OGGETTO NON POSSIDE IL COSTRUTTORE DI DEFAULT (O SE SI VUOLE INVOCARE UN COSTRUTTORE SPECIFICO)
Constructor<?> constructor = clazz.getConstructor(String.class);
Object obj = constructor.newInstance("HELLO");
//ALTRIMENTI
//Object obj = clazz.newInstance();
//INVOCO UN METODO CONCAT
Method method = clazz.getMethod("concat", String.class);
obj = method.invoke(obj, "WORLD");
System.out.println(obj);
}
catch (Exception e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}