codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
🎯

Kotlin

32 / 68 topics
31Kotlin Reflect API32Annotations in Kotlin33Metadata and Retention34Target Specifiers35Repeatable Annotations
Tutorials/Kotlin/Annotations in Kotlin
🎯Kotlin

Annotations in Kotlin

Updated 2026-04-20
2 min read

Introduction

Annotations in Kotlin are a powerful feature that allows you to add metadata to your code. They can be used for various purposes, such as documentation, configuration, and runtime processing. This tutorial will cover the basics of annotations in Kotlin, including how to define, use, and process them.

Defining Annotations

An annotation is a way to attach metadata to declarations (classes, functions, properties, etc.). In Kotlin, you can define an annotation using the annotation keyword. Here's a simple example:

annotation class MyAnnotation(val value: String)

In this example, we've defined an annotation called MyAnnotation that takes a single string parameter.

Using Annotations

Annotations can be applied to various declarations in Kotlin, such as classes, functions, properties, and parameters. Here's how you can use the MyAnnotation defined above:

@MyAnnotation("Hello, World!")
class MyClass {
    @MyAnnotation("This is a property")
    var myProperty: String = "Initial Value"

    @MyAnnotation("This is a function")
    fun myFunction() {
        println(myProperty)
    }
}

In this example, we've applied the MyAnnotation to a class, a property, and a function.

Targeting Specific Declarations

By default, an annotation can be used on any declaration. However, you can specify which declarations it should target using the @Target annotation. Here's an example:

import kotlin.annotation.AnnotationTarget.*

@Target(CLASS, FUNCTION)
annotation class MyAnnotation(val value: String)

In this example, the MyAnnotation can only be used on classes and functions.

Retention Policy

Annotations can have different retention policies, which determine when they are available. The retention policy is specified using the @Retention annotation. Here's an example:

import kotlin.annotation.Retention
import kotlin.annotation.RetentionPolicy.*

@Retention(RUNTIME)
annotation class MyAnnotation(val value: String)

In this example, the MyAnnotation will be retained at runtime, allowing you to process it using reflection.

Processing Annotations

Annotations can be processed at compile time or runtime. In Kotlin, you can use reflection to process annotations at runtime. Here's an example:

import kotlin.reflect.full.findAnnotations

fun main() {
    val myClass = MyClass::class
    val annotation = myClass.findAnnotations<MyAnnotation>().firstOrNull()
    println(annotation?.value) // Output: Hello, World!
}

In this example, we've used reflection to find the MyAnnotation on the MyClass class and print its value.

Best Practices

  1. Keep Annotations Lightweight: Annotations should not contain complex logic or large amounts of data.
  2. Use Annotations for Metadata: Annotations are best suited for adding metadata that can be processed at compile time or runtime.
  3. Document Annotations: Always document the purpose and usage of your annotations to make them easier to understand and use.

Conclusion

Annotations in Kotlin provide a powerful way to add metadata to your code, which can be used for various purposes such as documentation, configuration, and runtime processing. By understanding how to define, use, and process annotations, you can enhance the functionality and maintainability of your Kotlin applications.


PreviousKotlin Reflect APINext Metadata and Retention

Recommended Gear

Kotlin Reflect APIMetadata and Retention