> ## Documentation Index
> Fetch the complete documentation index at: https://docs.syntblaze.com/llms.txt
> Use this file to discover all available pages before exploring further.

# Kotlin Annotation Class

An annotation class in Kotlin is a specialized class used to attach metadata to declarations and expressions. It acts as a declarative marker that can be processed by the compiler, development tools, or at runtime via reflection, without directly altering the execution logic of the annotated code.

## Syntax and Declaration

An annotation class is defined using the `annotation` modifier preceding the `class` keyword.

```kotlin theme={"dark"}
// Parameterless annotation
annotation class Marker

// Annotation with parameters
annotation class Configuration(
    val retries: Int,
    val strategy: String = "DEFAULT"
)
```

## Constructor Constraints

Annotation classes have strict structural limitations enforced by the Kotlin compiler. If an annotation class defines a primary constructor, its parameters must adhere to the following rules:

* All parameters must be declared as read-only (`val`).
* Parameters cannot be nullable.
* Default values are permitted and must be compile-time constants.

**Allowed Parameter Types:**

* Primitive types (`Int`, `Long`, `Double`, `Boolean`, etc.)
* `String`
* Classes (`KClass`)
* Enums
* Other annotation classes
* Arrays of the types listed above

```kotlin theme={"dark"}
import kotlin.reflect.KClass

enum class Level { HIGH, LOW }

annotation class Dependency(val component: KClass<*>)

// Complex annotation demonstrating allowed types
annotation class MetaConfig(
    val id: String,
    val level: Level,
    val dependencies: Array<Dependency>
)
```

## Meta-Annotations

The behavior, visibility, and applicability of an annotation class are governed by meta-annotations—annotations applied directly to the annotation class declaration.

### `@Target`

Specifies the syntactic entities to which the annotation can be applied. It accepts one or more `AnnotationTarget` enum values.

```kotlin theme={"dark"}
@Target(AnnotationTarget.CLASS, AnnotationTarget.FUNCTION, AnnotationTarget.VALUE_PARAMETER)
annotation class Restricted
```

### `@Retention`

Determines the lifecycle of the annotation, dictating whether it is discarded or retained in the compiled output. It accepts an `AnnotationRetention` enum value:

* `SOURCE`: Discarded by the compiler; not present in the compiled binary.
* `BINARY`: Stored in the compiled class files but not visible through runtime reflection.
* `RUNTIME` (Default): Stored in the compiled class files and accessible at runtime via reflection.

```kotlin theme={"dark"}
@Retention(AnnotationRetention.SOURCE)
annotation class CompilerDirective
```

### `@Repeatable`

Allows the same annotation to be applied multiple times to a single element.

```kotlin theme={"dark"}
@Repeatable
@Target(AnnotationTarget.CLASS)
annotation class Tag(val name: String)

@Tag("Service")
@Tag("Singleton")
class NetworkManager
```

### `@MustBeDocumented`

Instructs documentation generation tools (like Dokka) to include the annotation in the generated public API documentation of the annotated elements.

## Instantiation Mechanics

Annotations are primarily instantiated by applying them to code elements using the `@` symbol. When applying an annotation that requires a class reference, Kotlin uses the `::class` syntax.

```kotlin theme={"dark"}
import kotlin.reflect.KClass

// Target class for the annotation reference
class NetworkManager 

@Target(AnnotationTarget.CLASS)
annotation class Injectable(val type: KClass<*>)

@Injectable(type = NetworkManager::class)
class ApiClient
```

If an annotation is used as a parameter within another annotation, the `@` prefix is omitted for the nested annotation.

```kotlin theme={"dark"}
@Target(AnnotationTarget.CLASS)
annotation class Route(val path: String)

@Target(AnnotationTarget.CLASS)
annotation class Controller(
    val name: String,
    val defaultRoute: Route // Nested annotation
)

@Controller(
    name = "UserController",
    defaultRoute = Route("/api/users") // No '@' prefix required here
)
class UserController
```

Since Kotlin 1.5.30, annotation classes can also be instantiated directly using standard object creation syntax by calling their constructors. This is particularly useful when working with reflection, custom metadata processing, or generating annotations dynamically.

```kotlin theme={"dark"}
annotation class Configuration(
    val retries: Int,
    val strategy: String = "DEFAULT"
)

// Direct instantiation
val configInstance = Configuration(retries = 3, strategy = "RETRY")
```

<div
  style={{ 
display: "flex", 
justifyContent: "space-between", 
alignItems: "center", 
maxWidth: "754px", 
padding: "1rem 0",
marginBottom: "24px"
}}
>
  <span style={{ fontWeight: "bold", fontSize: "1.25rem", color: "var(--tw-prose-headings)", fontFamily: "Inter, ui-sans-serif, system-ui, sans-serif" }}>Tired of Poor Kotlin Skills? Fix That With Deep Grasping!</span>

  <a
    href="https://syntblaze.com"
    target="_blank"
    style={{ 
  marginLeft: "24px",
  textDecoration: "none", 
  backgroundColor: "#007AFF",
  color: "#ffffff", 
  padding: "6px 16px", 
  borderRadius: "16px",
  fontSize: "0.9rem",
  fontWeight: "600",
  textAlign: "center",
  transition: "background-color 0.2s ease"
}}
  >
    Learn More
  </a>
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/skill-tracking.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=b9b0305c93bb501c9e767b5c76c88835" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/skill-tracking.png" />

  <img src="https://mintcdn.com/syntblazellc/23tyuOzaWS88qFlc/images/nuggets.png?fit=max&auto=format&n=23tyuOzaWS88qFlc&q=85&s=c86c80197299762989e9b882419b2109" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/nuggets.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/bite-sized-exercises.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=a65f9a38c37ff28ab73ed783c53c60e3" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/bite-sized-exercises.png" />
</div>

<div style={{ display: "flex", gap: "12px", flexWrap: "wrap", marginTop: "12px" }}>
  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/mastery-chain.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=748a1763454713e679260fbb95f154a2" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/mastery-chain.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-previews.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=242f61448ff5dd6deaaab2dccc13b507" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-previews.png" />

  <img src="https://mintcdn.com/syntblazellc/-L0ums_2lctDSZ1l/images/element-explanations.png?fit=max&auto=format&n=-L0ums_2lctDSZ1l&q=85&s=cf0fc1c31f9cd0fc26716781be05fbc9" style={{ width: "30%", minWidth: 60 }} width="621" height="1344" data-path="images/element-explanations.png" />
</div>
