Phương Nguyễn
Phương Nguyễn

Reputation: 8905

Get generic interface class object

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.

Upvotes: 2

Views: 562

Answers (2)

Jo&#227;o Silva
Jo&#227;o Silva

Reputation: 91349

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.

Upvotes: 3

Sean Owen
Sean Owen

Reputation: 66886

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.

Upvotes: 6

Related Questions