# Reified T - getting Class\<T\>

**URL:** <https://discuss.kotlinlang.org/t/reified-t-getting-class-t/1644>\
**Category:** Support\
**Created:** [May 4, 2016, 5:56am UTC](https://discuss.kotlinlang.org/t/reified-t-getting-class-t/1644 "2016-05-04T05:56:18Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![sksamuel](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sksamuel/32/1904_2.png) [@sksamuel](https://discuss.kotlinlang.org/u/sksamuel)\
**Post date:** [May 4, 2016, 5:56am UTC](https://discuss.kotlinlang.org/t/reified-t-getting-class-t/1644/1 "2016-05-04T05:56:18Z")

</div>

If I have a reified type T, how can I get a java.lang.Class for it. I can get a Kotlin KClass, and from that use Class.forName, but I imagine there’s an easier way?

final inline fun foo() :Class = Class.forName(T::class.qualifiedName) as Class

I need it for a library that requires a Class

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [May 4, 2016, 7:04am UTC](https://discuss.kotlinlang.org/t/reified-t-getting-class-t/1644/2 "2016-05-04T07:04:57Z")

</div>

> To obtain a Java class reference, use the .java property on a KClass instance.

[https://kotlinlang.org/docs/reference/reflection.html#class-references](https://kotlinlang.org/docs/reference/reflection.html#class-references)

---

<div class="post-metadata">

**Author:** ![sksamuel](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sksamuel/32/1904_2.png) [@sksamuel](https://discuss.kotlinlang.org/u/sksamuel)\
**Post date:** [May 4, 2016, 7:19am UTC](https://discuss.kotlinlang.org/t/reified-t-getting-class-t/1644/3 "2016-05-04T07:19:00Z")

</div>

T::class.java gives me an error in intellij, and T::class.javaClass returns a Class\<KClass\>

Try it online:

inline fun foo(): Unit {  
val e = E::class.java  
}

On-the-fly type checking  
A multi-language Hello.kt  
Warning:(2, 6) Variable ‘e’ is never used  
Error:(2, 19) Type parameter bound for T in val kotlin.reflect.KClass.java: java.lang.Class  
is not satisfied: inferred type E is not a subtype of kotlin.Any

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [May 4, 2016, 7:48am UTC](https://discuss.kotlinlang.org/t/reified-t-getting-class-t/1644/4 "2016-05-04T07:48:48Z")

</div>

```
inline fun <reified T:Any> foo() = T::class.java

fun main(args: Array<String>) {
    println("Java class: " + foo<String>())
}

```

---

<div class="post-metadata">

**Author:** ![sksamuel](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/sksamuel/32/1904_2.png) [@sksamuel](https://discuss.kotlinlang.org/u/sksamuel)\
**Post date:** [May 5, 2016, 4:19pm UTC](https://discuss.kotlinlang.org/t/reified-t-getting-class-t/1644/5 "2016-05-05T16:19:00Z")

</div>

Thanks for the reply. The key thing then is that the type parameter must be bound by Any.

---

<div class="post-metadata">

**Author:** ![fvasco](https://sea1.discourse-cdn.com/flex019/user_avatar/discuss.kotlinlang.org/fvasco/32/1961_2.png) [@fvasco](https://discuss.kotlinlang.org/u/fvasco)\
**Post date:** [May 6, 2016, 7:06am UTC](https://discuss.kotlinlang.org/t/reified-t-getting-class-t/1644/6 "2016-05-06T07:06:13Z")

</div>

Yes,  
and sounds like a bug…
