146

In Java we can resolve a variable's class through getClass() like something.getClass(). In Kotlin I am aware of something.javaClass which is nice but I want to be able to get the KClass in a similar way. I've seen the Something::class syntax but this is not what I need. I need to get the KClass of a variable. Does such functionality exist?

Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62
Jire
  • 9,680
  • 14
  • 52
  • 87

7 Answers7

229

The easiest way to achieve this since Kotlin 1.1 is the class reference syntax:

something::class

If you use Kotlin 1.0, you can convert the obtained Java class to a KClass instance by calling the .kotlin extension property:

something.javaClass.kotlin
Alexander Udalov
  • 31,429
  • 6
  • 80
  • 66
14

EDIT: See comments, below, and answer from Alexander, above. This advice was originally for Kotlin 1.0 and it seems is now obsolete.

Since the language doesn't support a direct way to get this yet, consider defining an extension method for now.

fun<T: Any> T.getClass(): KClass<T> {
    return javaClass.kotlin
}

val test = 0
println("Kotlin type: ${test.getClass()}")

Or, if you prefer a property:

val<T: Any> T.kClass: KClass<T>
    get() = javaClass.kotlin

val test = 0
println("Kotlin type: ${test.kClass}")
dherman
  • 495
  • 6
  • 7
  • 1
    You can skip the `java` bit and go straight Kotlin: `fun T.getClass(): KClass = this::class` – TWiStErRob May 14 '18 at 19:00
  • 1
    This answer should be updated to reflect the changes in 1.1. Now it is possible to get the class using `someClass::class` – Max Jul 10 '18 at 03:41
12

Here's my solution

val TAG = javaClass.simpleName

With javaClass.simpleName you can obtain your class name. Also the above example is very useful for android developers to declare on top of the class as an instance variable for logging purposes.

Kashif Anwaar
  • 728
  • 7
  • 14
3

I think this is the better solution. Found here https://www.techiedelight.com/determine-class-name-in-kotlin/

fun main() {

val s = "Kotlin"

println(s::class)                           // class kotlin.String
println(s::class.qualifiedName)             // kotlin.String
println(s::class.simpleName)                // String

}

  • 1
    As it’s currently written, your answer is unclear. Please [edit] to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Feb 09 '23 at 05:18
2

Here are different Implementations to get class names. You can utilize it as per your requirements.

import kotlin.reflect.KClass

val <T : Any > T.kClassName: KClass<out T>
get() {
    return javaClass.kotlin
}

Here we can get the class name in kotlin

val <T : Any > T.classNameKotlin: String?
get() {
    return javaClass.kotlin.simpleName
}

Here we can get the class name in kotlin

val <T : Any > T.classNameJava: String
get() {
    return javaClass.simpleName
}

Here are the outputs to the following operations.

fun main(){

val userAge = 0

println(userAge.kClassName) 
Output: class java.lang.Integer (Kotlin reflection is not available)

println(userAge.classNameKotlin)
Output: Int

println(userAge.classNameJava)
Output: Integer
}
Muhammad Maqsood
  • 1,622
  • 19
  • 25
2

Since Kotlin 1.5.21 (org.jetbrains.kotlin:kotlin-stdlib:1.5.21)

val TAG = javaClass.simpleName will work no more!

Error "Not enough information to infer type variable T"

/**
 * Returns the runtime Java class of this object.
 */
public inline val <T : Any> T.javaClass: Class<T>
    @Suppress("UsePropertyAccessSyntax")
    get() = (this as java.lang.Object).getClass() as Class<T>

@Deprecated("Use 'java' property to get Java class corresponding to this Kotlin class or cast this instance to Any if you really want to get the runtime Java class of this implementation of KClass.", ReplaceWith("(this as Any).javaClass"), level = DeprecationLevel.ERROR)
public inline val <T : Any> KClass<T>.javaClass: Class<KClass<T>>
    @JvmName("getRuntimeClassOfKClassInstance")
    @Suppress("UsePropertyAccessSyntax")
    get() = (this as java.lang.Object).getClass() as Class<KClass<T>>

You can try now YourClassName::class.java.simpleName

CryptoCode
  • 945
  • 6
  • 19
1

In Kotlin:

val className = serviceClass.javaClass.name
β.εηοιτ.βε
  • 33,893
  • 13
  • 69
  • 83