public class ClassTest {
public static void main(String[]arg)
{
String className="ClassTest";
try
{
Class c=Class.forName(className);
System.out.println(c);
}
catch(Exception ex)
{
System.out.println("Hello");
}
}
}
public static void main(String[]arg)
{
String className="ClassTest";
try
{
Class c=Class.forName(className);
System.out.println(c);
}
catch(Exception ex)
{
System.out.println("Hello");
}
}
}
Class.forName(className, true, currentLoader)
where currentLoader denotes the defining class loader of the current class.
For example, the following code fragment returns the runtime Class descriptor for the class named java.lang.Thread: Class t = Class.forName("java.lang.Thread")
A call to forName("X") causes the class named X to be initialized.
Parameters:
className - the fully qualified name of the desired class. //注意这里
Returns:
the Class object for the class with the specified name.Class c=Class.forName(className);//className应包括包名称, 如果你的package是 my, 这里的className就应该是my.ClassTest