Create an object from a string in Java
Description
The following code shows how to create an object from a string.
Example
/* w ww . j a v a 2 s.c om*/
public class Main {
public static void main(String[] argv) throws Exception {
Class[] classParm = null;
Object[] objectParm = null;
try {
String name = "java.lang.String";
Class cl = Class.forName(name);
java.lang.reflect.Constructor co = cl.getConstructor(classParm);
System.out.println(co.newInstance(objectParm));
} catch (Exception e) {
e.printStackTrace();
}
}
}
The code above generates the following result.
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »