0Pricing
Kotlin Academy · Lesson

Reflection Basics

Inspect types at runtime.

What Is Reflection?

Reflection is the ability of a program to inspect and manipulate its own structure at runtime: classes, properties, functions, and annotations.

Kotlin reflection lives in the kotlin.reflect package and centers on KClass.

Getting a KClass

Use the ::class syntax on a type or an instance to obtain its KClass object.

class Robot

fun main() {
    val fromType = Robot::class
    val fromInstance = Robot()::class
    println(fromType.simpleName)
    println(fromInstance.simpleName)
}

All lessons in this course

  1. Using Annotations
  2. Defining Annotations
  3. Reflection Basics
  4. Practical Reflection
← Back to Kotlin Academy