import java.lang.reflect.Constructor;
import java.util.Arrays;
class Example
{
System.
out.
println("(String): " + a
); // ...
}
public Example
(int a,
String b
) { System.
out.
println("(int, String): " + a
+ ", " + b
); // ...
}
System.
out.
println("(String, String): " + a
+ ", " + b
); // ...
}
static Example create
(Object...
params) { try {
Class[] paramTypes = new Class[params.length];
for (int n = 0; n < params.length; ++n) {
Class cls = params[n].getClass();
if (cls.
equals(Integer.
class)) { // You may need this (for int and other primitives) } else {
paramTypes[n] = cls;
}
}
Constructor ctor
= Example.
class.
getConstructor(paramTypes
); return (Example)ctor.newInstance(params);
System.
out.
println(e.
getMessage()); e.
printStackTrace(System.
out); }
}
public static void main
(String[] args
) {
Example e1 = Example.create("foo");
Example e2 = Example.create(1, "foo");
Example e3 = Example.create("foo", "bar");
}
}