Annotations in Kotlin
From the Kotlin cheat sheet · Annotations & Reflection · verified Jul 2026
Annotations
Creating and using annotations
kotlin
// Annotation declaration
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION)
@Retention(AnnotationRetention.RUNTIME)
@MustBeDocumented
annotation class MyAnnotation(
val name: String,
val version: Int = 1
)
// Using annotations
@MyAnnotation(name = "Example", version = 2)
class AnnotatedClass {
@Deprecated("Use newMethod instead")
fun oldMethod() {}
@JvmStatic
fun staticMethod() {}
@Throws(IOException::class)
fun riskyMethod() {
throw IOException("Error")
}
}
// Built-in annotations
class Example {
@JvmField
val field = "Public field in Java"
@JvmOverloads
fun method(a: String = "default") {}
@Suppress("UNCHECKED_CAST")
fun uncheckedCast(obj: Any): List<String> {
return obj as List<String>
}
}💡 @Target specifies where annotation can be used
⚡ @Retention determines if annotation is available at runtime
📌 Reflection requires kotlin-reflect dependency
🟢 Use @Jvm* annotations for Java interoperability