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

34 / 68 topics
31Kotlin Reflect API32Annotations in Kotlin33Metadata and Retention34Target Specifiers35Repeatable Annotations
Tutorials/Kotlin/Target Specifiers
🎯Kotlin

Target Specifiers

Updated 2026-04-20
3 min read

Target Specifiers

In Kotlin, target specifiers are a powerful feature that allows you to specify where and how annotations should be applied. This is particularly useful when working with reflection and other advanced features of the language. Understanding target specifiers can greatly enhance your ability to write robust and maintainable code.

Introduction to Annotations in Kotlin

Annotations in Kotlin are metadata markers that provide additional information about classes, functions, properties, parameters, etc. They do not affect the execution of the program but can be used by tools, compilers, or at runtime through reflection.

Basic Annotation Example

annotation class MyAnnotation(val value: String)

@MyAnnotation("Hello")
fun greet() {
    println("World!")
}

In this example, @MyAnnotation is applied to the greet function. The annotation provides a string value "Hello".

Target Specifiers Overview

Target specifiers allow you to restrict where an annotation can be used. By default, annotations can be applied to almost any Kotlin declaration, but sometimes you might want to limit their usage to specific targets.

Common Target Specifiers

  • CLASS: Applies to classes, interfaces, objects, and enums.
  • FUNCTION: Applies to functions.
  • PROPERTY: Applies to properties (including backing fields).
  • FIELD: Applies to backing fields of properties.
  • CONSTRUCTOR: Applies to constructors.
  • PARAMETER: Applies to function or constructor parameters.
  • SETTER: Applies to property setters.
  • GETTER: Applies to property getters.
  • FILE: Applies to the file containing the declaration.

Using Target Specifiers

To specify a target, you use the target keyword followed by one or more target specifiers in parentheses. Multiple targets can be specified using commas.

Example with Target Specifier

@Target(AnnotationTarget.FUNCTION)
annotation class MyFunctionAnnotation(val value: String)

@MyFunctionAnnotation("This is a function annotation")
fun exampleFunction() {
    println("Example function.")
}

In this example, MyFunctionAnnotation can only be applied to functions.

Advanced Usage of Target Specifiers

Multiple Targets

You can specify multiple targets for an annotation by listing them in the target specifier.

@Target(AnnotationTarget.PROPERTY, AnnotationTarget.FIELD)
annotation class MyPropertyAnnotation(val value: String)

class MyClass {
    @MyPropertyAnnotation("This is a property annotation")
    var myProperty: Int = 0
}

Here, MyPropertyAnnotation can be applied to properties and their backing fields.

Targeting Constructors

Annotations can also be applied to constructors using the CONSTRUCTOR target specifier.

@Target(AnnotationTarget.CONSTRUCTOR)
annotation class MyConstructorAnnotation(val value: String)

class MyClass @MyConstructorAnnotation("This is a constructor annotation") constructor() {
    // Class body
}

Targeting Parameters

Parameters of functions or constructors can be annotated using the PARAMETER target specifier.

@Target(AnnotationTarget.PARAMETER)
annotation class MyParameterAnnotation(val value: String)

fun exampleFunction(@MyParameterAnnotation("This is a parameter annotation") param: Int) {
    println(param)
}

Targeting File-Level Annotations

Annotations can be applied at the file level using the FILE target specifier.

// File: Example.kt
@file:Target(AnnotationTarget.FILE)
@file:MyFileAnnotation("This is a file annotation")

package com.example

annotation class MyFileAnnotation(val value: String)

class MyClass {
    // Class body
}

In this example, MyFileAnnotation applies to the entire file.

Best Practices for Using Target Specifiers

  1. Be Specific: Always specify the target specifier if your annotation has a limited scope of usage. This prevents accidental misuse and makes your code more readable.
  2. Document Usage: Clearly document where and how annotations should be used. This is especially important in large projects or when working with teams.
  3. Use Multiple Targets Wisely: While multiple targets can be useful, they can also lead to confusion if not managed properly. Ensure that the combination of targets makes sense for your use case.
  4. Avoid Overuse: Annotations should enhance clarity and functionality without cluttering your code. Use them judiciously.

Conclusion

Target specifiers in Kotlin provide a flexible way to control where annotations can be applied, enhancing both the safety and readability of your code. By understanding and using target specifiers effectively, you can write more robust and maintainable applications. Whether you're working with reflection or other advanced features, mastering target specifiers will undoubtedly improve your Kotlin development skills.

Additional Resources

  • Kotlin Documentation on Annotations
  • Kotlin Reflection Guide

By following this comprehensive guide, you should now have a solid understanding of target specifiers in Kotlin and be able to apply them effectively in your projects.


PreviousMetadata and RetentionNext Repeatable Annotations

Recommended Gear

Metadata and RetentionRepeatable Annotations