androidengineers.Book a session

Reflection and Runtime Features

Property & Function Reflection

article15 minHard

Prefer typed callable references when declarations are known

Property and function references let you pass known declarations as values without discovering them by name at runtime. Reflective inspection becomes useful when the declaration is not statically known.

data class Lesson(val title: String, val minutes: Int)

fun main() {
    val title = Lesson::title
    val lesson = Lesson("Kotlin", 25)
    check(title.get(lesson) == "Kotlin")
    val titles = listOf(lesson).map(Lesson::title)
    check(titles == listOf("Kotlin"))
}

Enumerating members with memberProperties requires JVM reflection support and may call getters with side effects. Reflection does not promise meaningful property order. Sort or explicitly specify order if output needs stability.

Exercise

Add a computed property and inspect the difference between reading known properties directly and enumerating all properties. Avoid evaluating private or sensitive values in diagnostic output.

Check: a property getter is executable code, not necessarily a passive field lookup.

Reference: Property references

YOUR LEARNING JOURNEY

0 of 110 available lessons completed

Progress saved in this browser. No account needed.
Property & Function Reflection | Kotlin Core Programming | Android Engineers