class Item
{
public static test()
{
}
}
String s = "package.Item";
Item o = (Item)Class.forName(s).cast(Item.class); // *
o.test();
but the marked line fails:
java.lang.ClassCastException: Cannot cast java.lang.Class to items.Item
class Item
{
public static test()
{
}
}
String s = "package.Item";
Item o = (Item)Class.forName(s).cast(Item.class); // *
o.test();
but the marked line fails:
java.lang.ClassCastException: Cannot cast java.lang.Class to items.Item
To create new instance you need to do the following
Class c = Class.forName("Item");
Item i = (Item)c.newInstance();
If you want to invoke static method you just call it on class instead of instance
Item.test();
Or you can use reflection without directly reference to class
Class c = Class.forName("Item");
Method method = c.getMethod("test");
method.invoke(null);