Open In App

Kotlin Reflection

Last Updated : 14 Jun, 2025
Summarize
Comments
Improve
Suggest changes
Share
Like Article
Like
Report

Kotlin Reflection is a powerful feature that allows a program to inspect and interact with its own structure at runtime. It enables developers to work with classes, properties, functions, constructors, and more all while the program is running.

Reflection is useful in many situations, such as:

  • Creating frameworks
  • Building libraries
  • Serialization and deserialization
  • Writing advanced test cases
  • Accessing private members (with care)

Kotlin supports both Java Reflection APIs and its own set of Kotlin Reflection APIs, making it flexible and interoperable with Java.

Kotlin Reflection Package

Kotlin's reflection tools are mainly available in the kotlin.reflect package. This package provides Kotlin-specific reflection features that are more concise and functional than Java’s reflection API.

To use Kotlin reflection, we need to add the following dependency in your build.gradle.kts (Module :app) file:

dependencies {
...
implementation("org.jetbrains.kotlin:kotlin-reflect")
}

Features of Kotlin reflection

  1. Access to Properties and Functions: Retrieve and manipulate properties and methods of classes during runtime.
  2. Support for Nullable Types: Reflect on nullability and type information.
  3. Integration with Java Reflection: Kotlin reflection works smoothly with Java’s reflection system.
  4. Introspection of JVM Code: Kotlin can reflect on code written in Java or other JVM languages.
  5. Simplified Syntax: Kotlin offers a more readable and functional syntax compared to Java reflection.

Class references

To get a reference to a class at runtime, Kotlin provides the class reference operator ::class. This is useful when you need information about the structure of a class. There are two types of Class References

  1. Unbounded Class Reference: Use ClassName::class to get a reference to a class known at compile time.
  2. Bounded Class Reference: Use object::class to get a reference to the actual type of an object instance, which is helpful when dealing with inheritance.

Example:

Kotlin
// A sample empty class
class ReflectionDemo

fun main() {
    // Reference obtained using class name
    val classRef = ReflectionDemo::class
    println("This is a class reference: $classRef")

    // Reference obtained using object
    val demoInstance = ReflectionDemo()
    val boundedRef = demoInstance::class
    println("This is a bounded class reference: $boundedRef")
}

Output:

This is a class reference: class ReflectionDemo
This is a bounded class reference: class ReflectionDemo

Function references

We can obtain a functional reference to every named function that is defined in Kotlin. This can be done by preceding the function name with the :: operator. These functional references can then be used as parameters to other functions.

Example: 

Kotlin
fun multiplyByThree(x: Int) = x * 3

fun main() {
    val numbers = listOf(1, 2, 3)
    // Function reference obtained using :: operator
    val result = numbers.map(::multiplyByThree)
    println(result)
}

Output:

[3, 6, 9]


Overloaded Functions

In the case of overloaded functions, we can either explicitly specify the type of the function or it can be implicitly determined from the content.

Example:

Kotlin
fun add(a: String, b: String): String = a + b
fun add(a: Int, b: Int): Int = a + b

val stringAdd: (String, String) -> String = ::add
val intAdd = ::add as (Int, Int) -> Int

fun main() {
    println(stringAdd("Kotlin", " Reflection"))
    println(intAdd(5, 3))
}

Output:

Kotlin Reflection
8

Property References

We can obtain property reference in a similar fashion as that of function, using the :: operator. If the property belongs to a class then the class-name should also be specified with the :: operator. These property references allow us to treat a property as an object that is, we can obtain their values using get function or modify it using set function.

Example: 

Kotlin
var x = 10

fun main() {
    val propRef = ::x
    println(propRef.get())
    propRef.set(20)
    println(x)
}

Output:

10
20

For class properties, use the class name or instance:

Kotlin
class Circle(var radius: Double)

fun main() {
    val circle = Circle(5.899)
    val radiusRef = Circle::radius
    println(radiusRef.get(circle))
}

Output: 

5.899 

Constructor References

The references to constructors of a class can be obtained in a similar manner as the references for methods and properties. These references can be used as references to a function which returns an object of that type. However, these uses are rare.

Example: 

Kotlin
class Person(val name: String)

fun main() {
    // Constructor Reference
    val constructorRef = ::Person
    val person = constructorRef("Alice")
    println(person.name)
}

Output:

Alice

Article Tags :

Similar Reads