VOOZH about

URL: https://www.geeksforgeeks.org/kotlin/kotlin-reflection/

⇱ Kotlin Reflection - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Kotlin Reflection

Last Updated : 12 Jul, 2025

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")
}

👁 Image

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:

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:

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:

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:

Output:

10
20

For class properties, use the class name or instance:

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:

Output:

Alice
Comment
Article Tags:
Article Tags:

Explore