Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get generic interface class object

Tags:

java

How to get class object of a generic interface? For example, Boolean.class, Date.class. But List<Boolean>.class doesn't seem to be syntax-valid.

like image 435
Phương Nguyễn Avatar asked Jul 03 '26 06:07

Phương Nguyễn


2 Answers

Just write List.class. List<Boolean> is not of a different type, at runtime. The generic type only exists at compile time. So this imaginary List<Boolean>.class would not exist to be used in your program.

There is no way to programmatically access the generic type, since it is not present in the byte code. It is only to help the compiler.

like image 50
Sean Owen Avatar answered Jul 05 '26 19:07

Sean Owen


Due to type erasure, your List<Boolean> will be converted to a simple raw List at runtime, thus, your best bet is to use List.class.

like image 37
João Silva Avatar answered Jul 05 '26 19:07

João Silva



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!