How to get parametrized Class instance

前端 未结 4 1917
别那么骄傲
别那么骄傲 2020-12-20 14:30

Since generics were introduced, Class is parametrized, so that List.class produces Class. This is clear.

What I am not able to figure out is how to get a in

4条回答
  •  南笙
    南笙 (楼主)
    2020-12-20 15:04

    The Class class is a run-time representation of a type. Since parametrized types undergo type erasure at runtime, the class object for Class would be the same as for Class> and Class>.

    The reason you cannot instantiate them using the .class notation is that this is a special syntax used for class literals. The Java Language Specification specifically forbids this syntax when the type is parametrized, which is why List.class is not allowed.

提交回复
热议问题