Why is using Collection.class illegal?

前端 未结 3 815
栀梦
栀梦 2020-12-08 19:02

I am puzzled by generics. You can declare a field like:

Class> clazz = ...

It seems logical that you could a

3条回答
  •  南方客
    南方客 (楼主)
    2020-12-08 19:29

    I agree with the other answers, and would like to explain one point further:

    Class objects represent classes that are loaded into the JVM memory. Each class object is actually an in-memory instance of a .class file. Java generics are not separate classes. They are just a part of the compile-time type-checking mechanism. Therefore, they have no run-time representation in a class object.

提交回复
热议问题