Answers for "java create class generic type from variable~"

1

get class from generic type java

class Foo<T> {
    final Class<T> typeParameterClass;

    public Foo(Class<T> typeParameterClass) {
        this.typeParameterClass = typeParameterClass;
    }

    public void bar() {
        // you can access the typeParameterClass here and do whatever you like
    }
Posted by: Guest on June-04-2021
-1

java define a generic class that produces

List list = new ArrayList();
list.add("abc");
list.add(new Integer(5)); //OK

for(Object obj : list){
	//type casting leading to ClassCastException at runtime
    String str=(String) obj; 
}
Posted by: Guest on April-27-2020

Code answers related to "java create class generic type from variable~"

Code answers related to "Java"

Java Answers by Framework

Browse Popular Code Answers by Language