I have a String
which has a name of a class say "Ex"
(no .class
extension). I want to assign it to a Class
variable, like this:
Class cls = (string).class
How can i do that?
I have a String
which has a name of a class say "Ex"
(no .class
extension). I want to assign it to a Class
variable, like this:
Class cls = (string).class
How can i do that?
Class<?> cls = Class.forName(className);
But your className
should be fully-qualified - i.e. com.mycompany.MyClass
Not sure what you are asking, but... Class.forname, maybe?
You can use the forName
method of Class
:
Class cls = Class.forName(clsName);
Object obj = cls.newInstance();
String clsName = "Ex"; // use fully qualified name
Class cls = Class.forName(clsName);
Object clsInstance = (Object) cls.newInstance();
Check the Java Tutorial trail on Reflection at http://java.sun.com/docs/books/tutorial/reflect/TOC.html for further details.