Reputation: 645
I want to pass a class as an argument to a function. But I have the string variable which contains name of that class.
Upvotes: 2
Views: 401
Reputation: 8468
This is the general method, you'll need to use a fully qualified class name.
myInstance.myMethod (Class.forName("fully.qualified.ClassName"));
Upvotes: 4
Reputation: 15886
Use static method of Class
class
yourMethod(other arguments,..., Class.forName("name of your class in string Formate.."));
this method return a Class class object for your class...
Upvotes: 3
Reputation: 258568
You can use reflection:
String className = "goo";
Class c = Class.forName(className);
foo(c);
Upvotes: 2
Reputation: 6487
try this:
Class.forName("Foo")
but the corresponding class must be in the classpath.
Upvotes: 2