1. What reflection is and when you need it
Imagine you are writing a program where it is impossible to list all types and scenarios upfront: plugins, serialization, a DI container, automatic log generation, import/export, universal diagnostics. In all such cases, you want to be able to ask the program: “So who are you, exactly?” — and get an answer.
Reflection is exactly about that: code that can look at other code (and at types) at runtime. It sounds like sci-fi, but in practice it is a tool that either helps a lot or very quickly turns a project into “dynamic mush” if you use it without discipline.
Static and runtime: two ways of thinking
In regular Kotlin you follow this principle: the compiler checked everything, so you can safely call methods. If you have an Expense, you write expense.amount, and the compiler guarantees that amount exists and has the right type.
With reflection you take a step aside: some decisions are made after startup. That means fewer compiler guarantees and more runtime checks. A handy rule is: if you can solve the task statically (with normal code), do it statically. Reflection is for cases where the static approach becomes inconvenient or impossible, and you are willing to pay with extra complexity and checks.
A small diagram to help your brain switch modes:
flowchart LR
A[Regular Kotlin code] --> B[Compiler checked types]
B --> C[Run: just execute]
D[Reflection] --> E[Run: discover type/members]
E --> F[Verify we found what we need]
F --> G[Only then execute]
2. KClass — the entry point to reflection
When you say “type” in Kotlin, you usually think of words like String, Int, Expense. But at runtime the program needs an object that represents that type. In Java that is Class<?>. In Kotlin it is KClass.
KClass is an object that describes a class (and, more broadly, a type) that you can work with as data: compare, read the name, use for diagnostics, and (in the next lectures) search for properties/methods and even call them.
Important: KClass is the starting point. Our task today is to learn how to obtain a KClass and do the safest, “without fanaticism”, operations with it.
How to get a KClass
There are two most useful ways to get a KClass, and they both look almost the same — as if Kotlin specifically wanted you to stay alert.
When the type is known upfront: SomeType::class
Here you are saying: “I know the type at compile time, just give me its KClass.”
import kotlin.reflect.KClass
data class Expense(val id: Int, val amount: Int, val category: String)
fun main() {
val cls: KClass<Expense> = Expense::class
println(cls) // class Expense
}
Here Expense::class has the type KClass<Expense>. This is handy when you build tables/maps “by type”, register handlers, or simply want to produce neat diagnostics.
When you have an object: obj::class
This approach is especially useful when an object comes in as a parameter typed as Any or a base interface, and you want to know its real type at runtime.
data class Expense(val id: Int, val amount: Int, val category: String)
fun main() {
val x: Any = Expense(1, 500, "food")
println(x::class.simpleName) // Expense
}
Here x::class is the “passport” of the real object, not what is written on the left of =.
A nuance about simpleName
simpleName is convenient, but it is nullable: anonymous objects and some local classes may not have a name. So a careful style is to always keep a fallback:
fun typeLabel(x: Any): String {
return x::class.simpleName ?: "<no name>"
}
fun main() {
val obj = object { val x = 1 }
println(typeLabel(obj)) // <no name>
}
What you can do with KClass without heavy reflection
For now we are not touching memberFunctions/memberProperties (that will be the next lecture). But even at the bare minimum, KClass is useful.
Diagnostics: printing the type name in logs
If you are building a console app, “smart” error/diagnostic messages are half the battle. KClass helps you write messages like “we got the wrong thing”.
fun describe(x: Any): String {
val name = x::class.simpleName ?: "<unknown>"
return "Got an object of type: $name"
}
fun main() {
println(describe("hi")) // Got an object of type: String
println(describe(123)) // Got an object of type: Int
}
Comparing types via KClass
Sometimes you want to say: “if this is exactly a string, do one thing; otherwise do something else”. Then you can compare KClass:
fun isExactlyString(x: Any): Boolean {
return x::class == String::class
}
fun main() {
println(isExactlyString("ok")) // true
println(isExactlyString(10)) // false
}
A type table: Map<KClass<*>, ...>
Sometimes it is convenient to build a small type-based lookup. Notice KClass<*>: it is a “class of an unknown type”, a safe form when the type parameter is not important to us.
import kotlin.reflect.KClass
fun main() {
val labels: Map<KClass<*>, String> = mapOf(
String::class to "string",
Int::class to "integer",
Double::class to "floating-point number"
)
val x: Any = 42
println(labels[x::class] ?: "something mysterious") // integer
}
This technique often shows up in “dispatching” logic and in diagnostics.
is and ::class == ... are not the same thing
A very common trap is to think that the check x is Animal is the same as x::class == Animal::class. It is not. And the difference matters: is accounts for inheritance, while comparing KClass checks the exact runtime class of the object.
Let’s take a look:
open class Animal
class Cat : Animal()
fun main() {
val a: Animal = Cat()
println(a is Animal) // true
println(a::class == Animal::class) // false
println(a::class == Cat::class) // true
}
In human terms: is means “is this an animal at all?”, while ::class == ... means “is this animal exactly of class Animal (no subclasses)?”.
To lock it in, here is a small table:
| Check | Meaning | Accounts for inheritance |
|---|---|---|
|
“x can be safely treated as T” | yes |
|
“x’s real class is exactly T” | no |
3. KClass in a console project: smarter diagnostics
Now we will apply KClass carefully in a “don’t break the architecture” style. We will not build business logic on reflection. We will add something that is almost always useful: a diagnostic description of what is happening.
Imagine (as in your practical project) that we have commands and a command parser. Commands are convenient to model via a sealed interface so the compiler helps you keep the list complete.
sealed interface Command
data class AddExpense(val amount: Int, val category: String) : Command
data object ListExpenses : Command
data class Help(val topic: String?) : Command
Command name for logs
Now let’s make a function that returns the “command name” for logs/debug output:
fun commandName(cmd: Command): String {
return cmd::class.simpleName ?: "<anonymous command>"
}
fun main() {
val cmd: Command = AddExpense(500, "food")
println(commandName(cmd)) // AddExpense
}
It is a small thing, but surprisingly nice: when you have a complex pipeline “input → parsing → execution → result”, this kind of diagnostics helps you not drown in your own code.
Logging an unexpected result type
If you return command execution results via a sealed interface, you can log which result variant arrived without printing the whole object.
sealed interface CommandResult {
data class Ok(val message: String) : CommandResult
data class Error(val message: String) : CommandResult
}
fun resultKind(r: CommandResult): String =
r::class.simpleName ?: "<unknown result>"
fun main() {
val r: CommandResult = CommandResult.Error("Bad input")
println(resultKind(r)) // Error
}
Again: this is not “magic”, just a careful improvement for readability and debugging.
4. When you need kotlin-reflect
Kotlin has an important practical nuance: full-featured reflection is a separate library kotlin-reflect, and it is not always included automatically. For small programs, this is a plus: fewer dependencies, faster startup, smaller size.
On the JVM, the compiler even has a separate option “do not add reflect to the classpath”. In the compiler options documentation there is a flag -no-reflect, which disables automatic inclusion of kotlin-reflect.jar.
Why this matters for you today: you can use ::class and basic operations (like comparing and getting a name) almost always. But as soon as you want a “list of class members” (members) or access to properties/methods, you will usually need kotlin-reflect.
An example with access to members appears even in the standard documentation about reified, where it uses T::class.members.
We intentionally do not go deeper into this right now: our goal is to learn the entry point (KClass) and not turn the project into a detective story of “why it doesn’t compile without yet another dependency”.
5. KClass and Java Class: how to convert types
Kotlin on the JVM lives next to Java, so you often need to “translate” a Kotlin type to a Java type and back. There are very straightforward bridges for that.
If you have a KClass, you can usually get the Java class via .java:
fun main() {
val javaCls = String::class.java
println(javaCls.name) // java.lang.String
}
And if you have an object, you can get the Java class via javaClass:
fun main() {
val x = "hi"
println(x.javaClass.name) // java.lang.String
}
This is especially useful when you use Java APIs that require Class<T> (for example, some factories, registries, older DI tools, and so on). But conceptually it is still more convenient to start from the Kotlin side: ::class and KClass.
6. Typical mistakes when getting started with KClass
Mistake No. 1: confusing is and ::class == ...::class.
This is the most common logical trap. The is check answers the question “can this object be treated as type T?” and accounts for inheritance. Comparing KClass checks the exact runtime class. If you mix them up, you will get strange branching: the code “doesn’t recognize” subclasses and behaves as if polymorphism was canceled.
Mistake No. 2: using simpleName!! and getting an NPE for no good reason.
For anonymous objects, local classes, and some special cases the name may be unavailable, so simpleName is nullable. In diagnostics and logs it is better to keep a fallback like ?: "<no name>" than to crash inside the logging system itself one day.
Mistake No. 3: assuming reflection is always available and being surprised that members doesn’t work.
You get the basic stuff with KClass almost always, but advanced reflection usually requires kotlin-reflect. If the project is built without it (or with a flag that forbids pulling it in automatically), some capabilities will be unavailable. The flag -no-reflect in JVM compiler options is exactly about this.
Mistake No. 4: building business logic on string type names “for flexibility”.
At the beginning it may seem that “if I decide based on simpleName, it will be flexible”. In practice this turns into brittle code: you rename a class and the logic changes. If you use KClass, it is better to compare KClass values directly, and keep strings for human-readable messages.
Mistake No. 5: doing reflection in a hot path.
Even if today you only use ::class and simpleName, keep a simple rule in mind: reflection is not free. If you start doing it inside large loops and collection processing “per record”, you may suddenly run into performance issues. For now this is just a warning for the future, not a reason to panic.
GO TO FULL VERSION