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. This is lesson 1 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 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
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 “Kotlin Reflection: KClass, KFunction, KProperty” lesson free?
Yes — any 3 lessons of the Kotlin Academy learning path, including “Kotlin Reflection: KClass, KFunction, KProperty”, 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 “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, so you can start here or from the beginning and move at your own pace. This is lesson 1 of 4.
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