How to Get the Class in Kotlin?
Last Updated :
28 Feb, 2022
Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a “better language” than Java, but still be fully interoperable with Java code. In this article, we will discuss how to get the class reference in Kotlin. Primarily, we will be working with reflection. So let's see what is
Reflection:
- It is a library that provides the ability to inspect code at runtime instead of compile-time. In Java, we can get a variable's class through getClass(), like something.getClass ().
- Let's see how to resolve a variable's class in Kotlin.
Example
Here are different Implementations to get class names.
Kotlin
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
Kotlin
val <T : Any > T.classNameKotlin: String?
get() {
return javaClass.kotlin.simpleName
}
Here are the outputs to the following operations.
Kotlin
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
}
Java's equivalent of resolving a variable's name is with the .getClass () method, for example, something.getClass(). In Kotlin, we can achieve the same thing with something.javaClass. To get a reference to the reflection class, we used to do something.class in Java, whose Kotlin equivalent is something::class. This returns a KClass. The special feature of this KClass is that it provides introspection capabilities quite similar to the abilities provided to Java's reflection class. Note that the KClass is different from Java's Class object. If you want to obtain a Java Class object from Kotlin's KClass, use the .java extension property:
val somethingkClass: KClass<Something> = Something: :class
val a: Class< Something> = somethingkClass. java
val b: Class < Something> = Something: : class . java
The latter example will be optimized by the compiler to not allocate an intermediate KClass instance. If you use Kotlin 1.0, you can convert the obtained Java class to a KClass instance by calling the .kotlin extension property, for example:
something.javaClass.kotlin.
As was just described, KClass provides you with introspection capabilities. Here are a few methods of KClass:
- isAbstract: True if this class is abstract
- isCompanion: True if this class is a companion object
- isData: True if this class is a data class
- isFinal: True if this class is final
- isInner: True if this class is an inner class
- isOpen: True if this class is open
Similar Reads
How to Work With Nested Class in Kotlin? A class is declared within another class then it is called a nested class. By default nested class is static so we can access the nested class property or variables using dot(.) notation without creating an object of the class. Syntax of declaration:Â class outerClass { ............ // outer class p
3 min read
Singleton Class in Kotlin Singleton Class in Kotlin is also called as the Singleton Object in Kotlin. Singleton class is a class that is defined in such a way that only one instance of the class can be created and used everywhere. Many times we create the two different objects of the same class, but we have to remember that
4 min read
How to Type Check an Object in Kotlin? Kotlin is a statically typed, general-purpose programming language developed by JetBrains, that has built world-class IDEs like IntelliJ IDEA, PhpStorm, Appcode, etc. It was first introduced by JetBrains in 2011 and is a new language for the JVM. Kotlin is an object-oriented language, and a âbetter
3 min read
Enum Classes in Kotlin In programming, sometimes we want a variable to have only a few specific values. For example, days of the week or card suits (like Heart, Spade, etc.). To make this possible, most programming languages support something called enumeration or enum.Enums are a list of named constants. Kotlin supports
4 min read
Kotlin Inline classes Inline classes are introduced by Kotlin since Kotlin 1.3 version to overcome the shortcomings of traditional wrappers around some types.These Inline classes add the goodness of Typealiases with the value range of the primitive data types. Let us suppose that we are selling some items and the cost is
4 min read