This article explores different ways to determine class name in Kotlin.

1. Using KClass

A KClass represents a class and provides introspection capabilities. Since Kotlin 1.1, you can use the ::class syntax to get the KClass. There are several different implementations to get the class name as per your requirements.

Download Code

 
Additionally, you can use KClass.js to access the JsClass instance corresponding to the class.

Download Code

2. Using Runtime Java class

The idea is to get the runtime Java class of the object with T.javaClass and use the abstract val properties name, simpleName, or canonicalName to get the name, simple name, or canonical name of the underlying class, respectively.

Download Code

 
Alternatively, you can convert the runtime Java class to a KClass instance by calling the Class<T>.kotlin extension property.

Download Code

That’s all about determining class name in Kotlin.