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

33 / 68 topics
31Kotlin Reflect API32Annotations in Kotlin33Metadata and Retention34Target Specifiers35Repeatable Annotations
Tutorials/Kotlin/Metadata and Retention
🎯Kotlin

Metadata and Retention

Updated 2026-04-20
3 min read

Metadata and Retention in Kotlin

Introduction

In this tutorial, we will explore the concepts of metadata and retention in Kotlin. These features are essential for developers who want to add additional information to their code and control how long that information is retained. We'll cover annotations, custom annotations, and how to use them effectively.

Understanding Metadata

Metadata refers to data that provides information about other data. In the context of programming languages like Kotlin, metadata can be used to describe classes, functions, properties, and more. This information can then be accessed at runtime using reflection or processed by tools during compilation.

Annotations in Kotlin

Annotations are a form of metadata that can be attached to declarations such as classes, functions, properties, parameters, and expressions. They provide additional information about the annotated elements without changing their behavior.

Built-in Annotations

Kotlin comes with several built-in annotations that serve various purposes:

  • @JvmField: Exposes a property as a field in Java.
  • @JvmStatic: Marks a member of an object or companion object as static, making it accessible from Java without an instance.
  • @Suppress: Suppresses compiler warnings and errors.

Custom Annotations

You can create your own annotations to add custom metadata. Here's how you define a simple annotation:

annotation class MyAnnotation(val value: String)

Using Annotations

Annotations are applied using the @ symbol followed by the annotation name. You can pass arguments if needed.

@MyAnnotation("Example")
class MyClass {
    @MyAnnotation("Property Example")
    var myProperty: Int = 0
}

Retention Policies

Retention policies determine how long an annotation is retained. Kotlin provides three retention policies:

  1. Source: The annotation is retained only in the source code and is discarded during compilation.
  2. Binary: The annotation is retained in the compiled .class files but not available at runtime.
  3. Runtime: The annotation is retained both in the compiled .class files and is accessible at runtime.

Setting Retention Policy

You can specify the retention policy using the @Retention annotation:

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

@Retention(RetentionPolicy.RUNTIME)
annotation class MyRuntimeAnnotation(val value: String)

@Retention(RetentionPolicy.BINARY)
annotation class MyBinaryAnnotation(val value: String)

@Retention(RetentionPolicy.SOURCE)
annotation class MySourceAnnotation(val value: String)

Accessing Annotations at Runtime

Annotations with a retention policy of RUNTIME can be accessed using reflection. Here's an example:

import java.lang.reflect.AnnotatedElement

fun printAnnotations(element: AnnotatedElement) {
    for (annotation in element.annotations) {
        println("Annotation: ${annotation.annotationClass.simpleName}, Value: ${annotation.javaClass.getMethod("value").invoke(annotation)}")
    }
}

@MyRuntimeAnnotation("Class Annotation")
class MyClass {
    @MyRuntimeAnnotation("Property Annotation")
    var myProperty: Int = 0
}

fun main() {
    val clazz = MyClass::class.java
    println("Annotations on class:")
    printAnnotations(clazz)

    val property = clazz.getDeclaredField("myProperty")
    println("\nAnnotations on property:")
    printAnnotations(property)
}

Output

Annotations on class:
Annotation: MyRuntimeAnnotation, Value: Class Annotation
Annotations on property:
Annotation: MyRuntimeAnnotation, Value: Property Annotation

Best Practices for Using Metadata and Retention

  1. Use Annotations Sparingly: Overusing annotations can make your code cluttered and harder to read.
  2. Document Annotations: Clearly document the purpose and usage of custom annotations to ensure they are used correctly.
  3. Choose Appropriate Retention Policies: Use SOURCE for compiler-related information, BINARY for tooling that processes compiled classes, and RUNTIME for runtime reflection.
  4. Avoid Runtime Reflection if Possible: While powerful, runtime reflection can lead to performance overhead. Consider other design patterns or libraries when possible.

Conclusion

Metadata and retention are powerful features in Kotlin that allow you to add meaningful information to your code and control how long that information is retained. By understanding annotations and retention policies, you can enhance the functionality and maintainability of your applications. Remember to use these features judiciously and document them well for future reference.

Additional Resources

  • Kotlin Annotations Documentation
  • Kotlin Reflection Documentation

By mastering metadata and retention in Kotlin, you'll be better equipped to write robust and maintainable code.


PreviousAnnotations in KotlinNext Target Specifiers

Recommended Gear

Annotations in KotlinTarget Specifiers