Java: How to create a class list from String

I want to create a list typed for a class name from a string. For instance,

String nameOfClass = "com.my.list.class"; List list = new ArrayList<nameOfclass>(); 

I only know the type of class that needs to be inserted into the List at run time in text format.

Is it possible?

+4
source share
1 answer

No.

General annotations occur only at compile time, they are erased from the runtime. When the program is executed, ArrayList does not know about its generic type.

Thus, type annotation is applicable only to the compiler. If you do not know the type at compile time (program development time), you will not be able to use them.

+9
source

All Articles