How to pass argument to Generic type class in java -
i have class similar given below
public class myclass<k,v>{ public myclass(properties prop){ map<k,v> mymap = new hashmap<k,v>(); } } i need invoke class passing string k , v myanotherclass , myanotherclass taken user input through file, how create myanotherclass given name file, , pass same myclass<string,myanotherclass> = new myclass<string,myanotherclass>() . best way achieve same.
any appreciated.
thanks
java erases types on compilation, need have user pass in type (by passing in class object of type) @ runtime.
import java.util.properties; import java.util.hashmap; import java.util.map; public class myclass<v extends class<v>> { public myclass(properties prop, v type){ map<string,v> mymap = new hashmap<string,v>(); } } will give need. called like
new myclass(properties, customer.class);
Comments
Post a Comment