-11

I need to get a Class from its name, something like this:

public void getClass(String nameOfClass){
  Class<?> c = Class.forName(nameOfClass); // Doesn't work
  doSomethingWith(c);
}

But the code doesn't work. How can I get a class based on the name?

assylias
  • 321,522
  • 82
  • 660
  • 783
SantiagoGG
  • 39
  • 2
  • 6
    Works for me. Maybe you can tell us what exactly "doesn't" work like tell us what error you get. – Aaron Digulla May 17 '13 at 11:53
  • possible duplicate of [Getting class by its name](http://stackoverflow.com/questions/10119956/getting-class-by-its-name) – fglez May 17 '13 at 12:15

1 Answers1

4

You need to pass fully qualified name into Class.forName(String name) method.

Like this

Class.forName("some.package.SomeClass");

Bitman
  • 1,996
  • 1
  • 15
  • 18
  • 1
    @kc2001 why do you need to get Class of the Class instance? 'static Class> forName(String className)' From javadoc: Returns the Class object associated with the class or interface with the given string name. – Bitman May 17 '13 at 12:02
  • @Ivan You're right. I wasn't paying enough attention. Let me finish this cup of coffee.... – kc2001 May 17 '13 at 12:11