Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

java return new instance of generic type

public static <E> void append(List<E> list, Class<E> cls) throws Exception {
    E elem = cls.newInstance();   // OK
    list.add(elem);
}
//You can invoke the append method as follows:
List<String> ls = new ArrayList<>();
append(ls, String.class);
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #java #return #instance #generic #type
ADD COMMENT
Topic
Name
1+7 =