0Pricing
Kotlin Academy · Lesson

Reflection Basics

Inspect types at runtime.

Reflection Basics is a free Kotlin Academy lesson on CoddyKit — lesson 3 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

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

Names of a Class

A KClass exposes simpleName and qualifiedName. The qualified name includes the package path.

class Engine

fun main() {
    val k = Engine::class
    println(k.simpleName)
    println(k.qualifiedName)
}

Inspecting Properties

memberProperties lists the properties of a class. Each is a KProperty you can ask for its name.

import kotlin.reflect.full.memberProperties

class Point(val x: Int, val y: Int)

fun main() {
    for (p in Point::class.memberProperties) {
        println(p.name)
    }
}

Reading Property Values

A KProperty1 can be called with an instance to read its value via get(instance).

import kotlin.reflect.full.memberProperties

class Point(val x: Int, val y: Int)

fun main() {
    val p = Point(3, 4)
    for (prop in Point::class.memberProperties) {
        println(prop.name + " = " + prop.get(p))
    }
}

Inspecting Functions

memberFunctions returns the callable functions of a class as KFunction objects. You can read each function's name.

import kotlin.reflect.full.memberFunctions

class Calculator {
    fun add(a: Int, b: Int) = a + b
    fun negate(a: Int) = -a
}

fun main() {
    Calculator::class.memberFunctions
        .filter { it.name in listOf("add", "negate") }
        .forEach { println(it.name) }
}

Function References

The ::name syntax produces a callable reference of type KFunction. You can invoke it later with invoke or pass it around.

fun double(n: Int) = n * 2

fun main() {
    val ref = ::double
    println(ref.name)
    println(ref.invoke(21))
}

Creating Instances

createInstance() builds an object reflectively when the class has a no-argument constructor.

import kotlin.reflect.full.createInstance

class Widget {
    override fun toString() = "Widget"
}

fun main() {
    val w = Widget::class.createInstance()
    println(w)
}

Inspecting Constructors

constructors lists the class constructors. You can examine their parameters to learn what arguments they expect.

class Person(val name: String, val age: Int)

fun main() {
    val ctor = Person::class.constructors.first()
    ctor.parameters.forEach { println(it.name) }
}

Costs and Caveats

Reflection is powerful but:

  • Slower than direct calls
  • Bypasses compile-time safety
  • Requires the kotlin-reflect library on the JVM

Use it for frameworks and tooling, not hot loops.

The Reflection Toolbox

Key types to remember:

  • KClass — a class
  • KProperty — a property
  • KFunction — a function
  • KParameter — a parameter

Together they let you read structure dynamically.

Quick Check

Test your understanding of reflection basics.

Recap

You learned reflection fundamentals:

  • Obtaining a KClass via ::class
  • Listing memberProperties and memberFunctions
  • Reading property values and invoking function references
  • Building objects with createInstance()

Next: reading annotations dynamically.

Frequently asked questions

Is the “Reflection Basics” lesson free?

Yes — the full text of “Reflection Basics” is free to read here on the web, and the Kotlin Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Kotlin Academy course, upgrade to CoddyKit PRO.

What will I learn in “Reflection Basics”?

Inspect types at runtime. You practise Kotlin Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Kotlin Academy?

No prior experience is required. Kotlin Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 3 of 4, so you can start here or from the beginning and move at your own pace.

How long does the “Reflection Basics” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Kotlin Academy lesson?

Yes. Every Kotlin Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

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