Kotlin Reflection: KClass, KFunction, KProperty
Navigate class metadata using KClass and inspect members reflectively.
Kotlin Reflection: KClass, KFunction, KProperty is a free Kotlin Academy lesson on CoddyKit — lesson 1 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 lets code inspect and invoke its own structure at runtime: classes, functions, properties, annotations, constructors. Kotlin wraps the JVM reflection API with its own richer types under the kotlin.reflect package.
Getting a KClass
Use ::class on a type or an instance to obtain a KClass reference:
val klass: KClass<String> = String::class
val instance: KClass<out Any> = "hello"::class
println(klass.simpleName) // "String"KClass Properties
KClass exposes metadata about the class:
val klass = String::class
println(klass.simpleName) // "String"
println(klass.qualifiedName) // "kotlin.String"
println(klass.isAbstract) // false
println(klass.constructors.size) // number of constructorsKFunction: Callable References
Use ::functionName to get a KFunction reference. You can inspect its parameters and call it:
fun greet(name: String) = "Hello, $name!"
val fn: KFunction<String> = ::greet
println(fn.name) // "greet"
println(fn.call("Kotlin")) // "Hello, Kotlin!"Inspecting Parameters
KFunction.parameters returns a list of KParameter objects with name, type, and optionality:
fun add(a: Int, b: Int) = a + b
::add.parameters.forEach {
println("${it.name}: ${it.type}")
}
// a: kotlin.Int
// b: kotlin.IntKProperty: Property References
Property references capture a property as a first-class object. KProperty0 is for properties without a receiver; KProperty1 is for member properties.
data class User(val name: String, val age: Int)
val prop: KProperty1<User, String> = User::name
val user = User("Alice", 30)
println(prop.get(user)) // "Alice"Mutable Properties with KMutableProperty
For var properties, the reference is a KMutableProperty1, which also has a set function:
class Box(var value: Int)
val prop: KMutableProperty1<Box, Int> = Box::value
val box = Box(10)
prop.set(box, 42)
println(box.value) // 42Listing All Members
KClass.members returns all visible members (functions + properties). KClass.declaredMemberProperties returns only the properties declared in that class.
data class Point(val x: Int, val y: Int)
Point::class.declaredMemberProperties.forEach {
println(it.name) // "x", "y"
}Creating Instances via KClass
Call KClass.createInstance() (requires a no-arg constructor) or use KClass.constructors for full control:
class Counter {
var count = 0
}
val c = Counter::class.createInstance()
c.count = 5
println(c.count) // 5Kotlin Reflect Dependency
Kotlin reflection requires the kotlin-reflect artifact (not included in the stdlib). Add it to your build:
// build.gradle.kts
implementation("org.jetbrains.kotlin:kotlin-reflect")Performance Note
Reflection is significantly slower than direct code. Cache KClass, KFunction, and KProperty references at startup. Avoid calling them in hot loops. For most tooling and framework code this cost is acceptable.
Quick Check
Which Kotlin type represents a reference to a member property and allows reading its value from an instance?
Recap: Kotlin Reflection
Key types:
KClass— class metadata, constructors, membersKFunction— callable references, parameters, invocationKProperty1/KMutableProperty1— property references, get/set- Requires
kotlin-reflecton the classpath - Cache references; avoid in hot paths
Frequently asked questions
Is the “Kotlin Reflection: KClass, KFunction, KProperty” lesson free?
Yes — the full text of “Kotlin Reflection: KClass, KFunction, KProperty” 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 “Kotlin Reflection: KClass, KFunction, KProperty”?
Navigate class metadata using KClass and inspect members reflectively. 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 1 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Kotlin Reflection: KClass, KFunction, KProperty” 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
- Kotlin Reflection: KClass, KFunction, KProperty
- Creating and Targeting Custom Annotations
- Reading Annotations at Runtime
- Reflection-Based Validation and Mapping