Getting Class type from String

Steven picture Steven · Mar 9, 2010 · Viewed 124.5k times · Source

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?

Answer

Bozho picture Bozho · Mar 9, 2010
Class<?> cls = Class.forName(className);

But your className should be fully-qualified - i.e. com.mycompany.MyClass