Java – How to pass class name in function arguments

java

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 a Class instance. Then you have to get the Method for the static method in question via getMethod, and invoke it via invoke.

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.