---
title: "Creating Custom Annotations in Java"  
description: "Creating Custom Annotations in Java"  
author: "Ravi Vishwakarma"  
published: 2025-04-24  
updated: 2025-04-24  
canonical: https://www.mindstick.com/interview/34070/creating-custom-annotations-in-java  
category: "java"  
tags: ["java"]  
reading_time: 4 minutes  

---

# Creating Custom Annotations in Java

In Java, you can create your very own annotations using the `@interface` keyword. These custom annotations may be applied to **classes**, **methods**, **fields**, or other elements, and you can retrieve and system them at runtime the use of mirrored image.

## 1. Basic Syntax

```java
public @interface MyAnnotation {
    String value();  // a required element
}
```

This defines an annotation named `MyAnnotation` that has a single required element called `value`.

## 2. Adding Metadata: `@Target` and `@Retention`

You should define **where** the annotation can be applied and **how long** it should be retained.

```java
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)  // Available at runtime via reflection
@Target(ElementType.METHOD)          // Can be applied to methods
public @interface MyAnnotation {
    String value();
    int count() default 1;           // Optional element with default value
}
```

- `@Retention(RetentionPolicy.RUNTIME)` — Makes the annotation available during runtime.
- `@Target(ElementType.METHOD)` — Limits annotation use to methods.

## 3. Using the Custom Annotation

```java
public class MyService {
    @MyAnnotation(value = "Run this", count = 3)
    public void doSomething() {
        System.out.println("Doing something...");
    }

    @MyAnnotation(value = "Another method")
    public void anotherMethod() {
        System.out.println("Another thing...");
    }
}
```

## 4. Accessing Custom Annotations Using Reflection

```java
import java.lang.reflect.Method;

public class AnnotationReader {
    public static void main(String[] args) {
        Class<MyService> clazz = MyService.class;

        for (Method method : clazz.getDeclaredMethods()) {
            if (method.isAnnotationPresent(MyAnnotation.class)) {
                MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
                System.out.println("Method: " + method.getName());
                System.out.println("  Value: " + annotation.value());
                System.out.println("  Count: " + annotation.count());
            }
        }
    }
}
```

## 5. Common `@Target` Values

| `ElementType` | Description |
| --- | --- |
| `TYPE` | Class, interface, enum |
| `FIELD` | Member variables |
| `METHOD` | Methods |
| `PARAMETER` | Parameters |
| `CONSTRUCTOR` | Constructors |
| `LOCAL_VARIABLE` | Local variables |

## Summary

- Use `@interface` to define your annotation.
- Use `@Retention` to specify when the annotation is available.
- Use `@Target` to limit where the annotation can be used.
- Use reflection to read annotations at runtime.

## Answers

### Answer by Ravi Vishwakarma

In Java, you can create your very own annotations using the `@interface` keyword. These custom annotations may be applied to **classes**, **methods**, **fields**, or other elements, and you can retrieve and system them at runtime the use of mirrored image.

## 1. Basic Syntax

```java
public @interface MyAnnotation {
    String value();  // a required element
}
```

This defines an annotation named `MyAnnotation` that has a single required element called `value`.

## 2. Adding Metadata: `@Target` and `@Retention`

You should define **where** the annotation can be applied and **how long** it should be retained.

```java
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Retention(RetentionPolicy.RUNTIME)  // Available at runtime via reflection
@Target(ElementType.METHOD)          // Can be applied to methods
public @interface MyAnnotation {
    String value();
    int count() default 1;           // Optional element with default value
}
```

- `@Retention(RetentionPolicy.RUNTIME)` — Makes the annotation available during runtime.
- `@Target(ElementType.METHOD)` — Limits annotation use to methods.

## 3. Using the Custom Annotation

```java
public class MyService {
    @MyAnnotation(value = "Run this", count = 3)
    public void doSomething() {
        System.out.println("Doing something...");
    }

    @MyAnnotation(value = "Another method")
    public void anotherMethod() {
        System.out.println("Another thing...");
    }
}
```

## 4. Accessing Custom Annotations Using Reflection

```java
import java.lang.reflect.Method;

public class AnnotationReader {
    public static void main(String[] args) {
        Class<MyService> clazz = MyService.class;

        for (Method method : clazz.getDeclaredMethods()) {
            if (method.isAnnotationPresent(MyAnnotation.class)) {
                MyAnnotation annotation = method.getAnnotation(MyAnnotation.class);
                System.out.println("Method: " + method.getName());
                System.out.println("  Value: " + annotation.value());
                System.out.println("  Count: " + annotation.count());
            }
        }
    }
}
```

## 5. Common `@Target` Values

| `ElementType` | Description |
| --- | --- |
| `TYPE` | Class, interface, enum |
| `FIELD` | Member variables |
| `METHOD` | Methods |
| `PARAMETER` | Parameters |
| `CONSTRUCTOR` | Constructors |
| `LOCAL_VARIABLE` | Local variables |

## Summary

- Use `@interface` to define your annotation.
- Use `@Retention` to specify when the annotation is available.
- Use `@Target` to limit where the annotation can be used.
- Use reflection to read annotations at runtime.


---

Original Source: https://www.mindstick.com/interview/34070/creating-custom-annotations-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
