> ## 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.

# Java Repeatable Annotation

A repeatable annotation in Java allows the same annotation type to be applied more than once to a single program element. Introduced in Java SE 8, it provides syntactic sugar that eliminates the need to manually group multiple annotations of the same type within an array-based container annotation.

Under the hood, the Java compiler automatically wraps the repeated annotations into a designated container annotation. To implement this, the architecture requires two distinct annotation interfaces: the **Repeatable Annotation** and the **Container Annotation**.

## 1. The Container Annotation

The container annotation must declare a `value()` element whose return type is an array of the repeatable annotation type.

```java theme={"dark"}
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Retention(RetentionPolicy.RUNTIME)
public @interface Configs {
    Config[] value();
}
```

## 2. The Repeatable Annotation

The annotation to be repeated must be meta-annotated with `@java.lang.annotation.Repeatable`. The value passed to `@Repeatable` is the `.class` literal of the container annotation.

```java theme={"dark"}
import java.lang.annotation.Repeatable;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;

@Repeatable(Configs.class)
@Retention(RetentionPolicy.RUNTIME)
public @interface Config {
    String key();
    String value();
}
```

## 3. Application Syntax

Once linked via the `@Repeatable` meta-annotation, the compiler allows the annotation to be stacked directly on a target element.

```java theme={"dark"}
@Config(key = "timeout", value = "5000")
@Config(key = "retries", value = "3")
public class NetworkClient {
    // Implementation
}
```

During compilation, the compiler transforms the above syntax into the legacy array format. The resulting bytecode is identical to:

`@Configs({@Config(key="timeout", value="5000"), @Config(key="retries", value="3")})`

## Reflection and Retrieval

Because the compiler wraps repeatable annotations inside the container annotation, Java 8 introduced the `getAnnotationsByType()` method in the Reflection API (`java.lang.reflect.AnnotatedElement`). This method automatically unpacks the container and returns an array of the repeated annotations.

```java theme={"dark"}
Class<NetworkClient> clazz = NetworkClient.class;

// Unpacks the container and returns the individual annotations
Config[] configs = clazz.getAnnotationsByType(Config.class);

for (Config config : configs) {
    System.out.println(config.key() + " = " + config.value());
}
```

If you use the older `getAnnotation(Config.class)` method on an element with repeated annotations, it will return `null` because the element is technically annotated with the container (`@Configs`), not the individual `@Config` annotation.

## Structural Constraints

To successfully compile a repeatable annotation, the Java compiler enforces strict rules regarding meta-annotations:

* **Target Compatibility:** The `@Target` of the container annotation must be a subset of, or identical to, the `@Target` of the repeatable annotation. If the container annotation can be applied to a `METHOD`, the repeatable annotation must also be applicable to a `METHOD`.
* **Retention Compatibility:** The `@Retention` policy of the container annotation must be equal to or broader than the retention policy of the repeatable annotation. You cannot have a `RUNTIME` repeatable annotation wrapped in a `SOURCE` container annotation.
* **Documented/Inherited:** If the repeatable annotation is marked with `@Documented` or `@Inherited`, the container annotation must also be marked with the exact same meta-annotations to preserve expected behavior.

<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 Java 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>
