I have an interface Action:
package action;
public interface Action {
public String act();
}
Class SimpleAction:
package action;
public class SimpleAction implements Action {
String action;
public SimpleAction() {}
public SimpleAction(String x) {
this.action = x;
}
@Override
public String act() {
return "Act method from -" + this.action;
}
}
Class ComplexAction:
package action;
public class ComplexAction implements Action{
String action;
public ComplexAction() {}
public ComplexAction(String x) {
this.action = x;
}
@Override
public String act() {
return "Act method from -" + this.action;
}
}
I want to create a function which takes the name of class and returns an object of that class. This is what I have so far in my function -
public static Action resultOfActMethod(String objclass){
Class clazz = Class.forName(objclass);
MethodHandles.Lookup lookup = MethodHandles.lookup();
MethodHandle mh = lookup.findConstructor(clazz,MethodType.methodType(void.class, String.class));
}
Figured it out.
You can do just this, it will give you an object, using the constructor with no arguments:
But if possible in your application, you should make a function which takes a class instead like this:
This is typesafe. You can use a specific return type. This will avoid a type cast at the caller's side.