I want to create a function that will perform some operation(most time occurring)
I created function like following
public void doSth()
{
//logic
ClassName.staticMethod();
//logic
}
In My application there are many times this function will be called. Only the particular line will be change. I decided to give a common function.
Now my question is: How do I pass the ClassName in function arguments so that function body use it dynamically?
Thanks
Best Answer
You can do that, via
Class.forName
which accepts a fully-qualified class name and returns aClass
instance. Then you have to get theMethod
for the static method in question viagetMethod
, and invoke it viainvoke
.But passing around class names as strings is a suspect design decision. I'd look at alternatives, such as using singletons rather than static methods and an interface, that sort of thing.