Saturday, August 18, 2018

How to set generic type at runtime in java

I saw some questions like this but different from my case.I want to set generated class at build time as generic type at runtime. here is code.

  //employee class is generated at build time 
  void function(Class employee)
  {  
      // I want to set class as generic type like this 
     List emps= new ArrayList(employee);
   }

how to do this?

Solved

You can't.

Generics are all mapped to java.lang.Object on compilation by a process called type erasure.

You can only build things that are checked at compile time: consider using Class notation.


No comments:

Post a Comment