Kotlin Academy · Lesson

Reflection Basics

Inspect types at runtime.

Lesson 3 of 413 steps

Reflection Basics is a free Kotlin Academy lesson on CoddyKit. This is lesson 3 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the Kotlin Academy learning path, and your progress syncs across the web and the CoddyKit app. The Kotlin Academy course includes 4 lessons in total.

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.

Free to start

Learn Kotlin with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
51
Lessons
203

Frequently Asked Questions

Is the “Reflection Basics” lesson free?

Yes — any 3 lessons of the Kotlin Academy learning path, including “Reflection Basics”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The Kotlin Academy course includes 4 lessons in total.

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, so you can start here or from the beginning and move at your own pace. This is lesson 3 of 4.

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