0Pricing
Kotlin Academy · Lesson

Defining Annotations

Create custom ones.

Declaring an Annotation

You define an annotation with the annotation class keywords. The simplest one carries no data and acts as a marker.

annotation class Beta

@Beta
fun newFeature() = "experimental"

fun main() {
    println(newFeature())
}

Annotation Parameters

Annotations can hold data through constructor parameters. Allowed types are limited: primitives, String, enums, other annotations, KClass, and arrays of those.

annotation class ApiVersion(val value: Int)

@ApiVersion(2)
fun endpoint() = "v2"

fun main() {
    println(endpoint())
}

All lessons in this course

  1. Using Annotations
  2. Defining Annotations
  3. Reflection Basics
  4. Practical Reflection
← Back to Kotlin Academy