this is my code
Class<?> clazz;
try {
clazz = Class.forName("classes.Pizza");
Object p2 = clazz.newInstance();
System.out.println(p2.test);
} catch (ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Error is "test cannot be resolved or is not a field" I want to get a string containing the class name and create a object with that type.. something like
String x = "Pizza"
x pizza1 = new x();
How do i do that ?
p2available for compiler? Does this type havetestfield?test? Or any other field you like to access?PizzaorCalzone. Those classes could implement a common interface with a methodvoid import(String fields)andString export()to read their fields from a string or write fields to a string. They are responsible for correctly reading/writing their own fields, so you don't have to worry about that outside of those classes. I'm skipping over a lot of details here, but I feel like I'm already getting a bit off-topic...