Why value of T::class.java is Class<out Int> in Kotlin?


#1

I define a class,It like this

class ResponseHandler<T : Any>(
    val typeClass: Class<out Int> = T::class.java,
    val start: () -> Unit = {},
    val success: (result: SuccessInfo<T>) -> Unit,
    val complete: () -> Unit = {}){/*...*/}

but why type of [T::class.java] is [Class<out Int>]
What i should do if i want to get the class of T ?
for example:

    ResponseHandler<User>(....).typeClass is User,not Int.

#2

You can’t use expressions like T::class.java in JVM due to type erasure. You have to explicitly pass the class as a parameter. The only way to avoid it is to use reified generics in inline methods like

inline fun <reified T: Any> createHandler(...) = ResponseHandler(T::class.java, ...)


#3

OK. Thank you for your reminder
I also think about this reason
Thank you!