---
title: "Working with Annotations in Java"  
description: "Working with Annotations in Java"  
author: "ICSM Computer"  
published: 2025-04-24  
updated: 2025-04-24  
canonical: https://www.mindstick.com/interview/34069/working-with-annotations-in-java  
category: "java"  
tags: ["java"]  
reading_time: 4 minutes  

---

# Working with Annotations in Java

**Annotations** in Java are a form of metadata. They provide data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate but are widely used in frameworks (like **Spring**, **JUnit**) for configuration and behavior control.

## 1. Built-in Annotations

Java provides several built-in annotations, such as:

- `@Override` – Ensures that a method is overriding a superclass method.
- `@Deprecated` – Marks a method/class as deprecated (shouldn’t be used).
- `@SuppressWarnings` – Tells the compiler to ignore specific warnings.

```java
public class Example {
    @Override
    public String toString() {
        return "Example";
    }

    @Deprecated
    public void oldMethod() {
        System.out.println("This method is deprecated");
    }
}
```

## 2. Creating Custom Annotations

You can define your own annotation using `@interface`.

### Example:

```java
import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
    String value() default "Default value";
}
```

- `@Retention` – Specifies how long the annotation is retained (e.g., `SOURCE`, `CLASS`, `RUNTIME`).
- `@Target` – Indicates where this annotation can be used (e.g., `METHOD`, `FIELD`, `TYPE`).

## 3. Using Custom Annotation

```java
public class TestAnnotationUsage {
    @MyAnnotation(value = "Hello Reflection")
    public void testMethod() {
        System.out.println("Running testMethod");
    }
}
```

## 4. Accessing Annotations with Reflection

You can use reflection to read and process annotations at runtime.

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

public class AnnotationProcessor {
    public static void main(String[] args) throws Exception {
        Class<?> clazz = TestAnnotationUsage.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("Annotation value: " + annotation.value());
            }
        }
    }
}
```

## 5. Common Framework Use Cases

- **Spring Framework** uses annotations like `@Autowired`, `@Service`, `@Controller`, `@RequestMapping`.
- **JUnit** uses `@Test`, `@BeforeEach`, `@AfterEach`.
- **Hibernate** uses `@Entity`, `@Table`, `@Column`.

## Summary Table

| Concept | Usage |
| --- | --- |
| `@interface` | Declares a custom annotation |
| `@Retention` | Defines how long the annotation is kept |
| `@Target` | Defines where the annotation can be applied |
| Reflection | Used to read annotations at runtime |

## Answers

### Answer by ICSM Computer

**Annotations** in Java are a form of metadata. They provide data about a program that is not part of the program itself. Annotations have no direct effect on the operation of the code they annotate but are widely used in frameworks (like **Spring**, **JUnit**) for configuration and behavior control.

## 1. Built-in Annotations

Java provides several built-in annotations, such as:

- `@Override` – Ensures that a method is overriding a superclass method.
- `@Deprecated` – Marks a method/class as deprecated (shouldn’t be used).
- `@SuppressWarnings` – Tells the compiler to ignore specific warnings.

```java
public class Example {
    @Override
    public String toString() {
        return "Example";
    }

    @Deprecated
    public void oldMethod() {
        System.out.println("This method is deprecated");
    }
}
```

## 2. Creating Custom Annotations

You can define your own annotation using `@interface`.

### Example:

```java
import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
    String value() default "Default value";
}
```

- `@Retention` – Specifies how long the annotation is retained (e.g., `SOURCE`, `CLASS`, `RUNTIME`).
- `@Target` – Indicates where this annotation can be used (e.g., `METHOD`, `FIELD`, `TYPE`).

## 3. Using Custom Annotation

```java
public class TestAnnotationUsage {
    @MyAnnotation(value = "Hello Reflection")
    public void testMethod() {
        System.out.println("Running testMethod");
    }
}
```

## 4. Accessing Annotations with Reflection

You can use reflection to read and process annotations at runtime.

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

public class AnnotationProcessor {
    public static void main(String[] args) throws Exception {
        Class<?> clazz = TestAnnotationUsage.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("Annotation value: " + annotation.value());
            }
        }
    }
}
```

## 5. Common Framework Use Cases

- **Spring Framework** uses annotations like `@Autowired`, `@Service`, `@Controller`, `@RequestMapping`.
- **JUnit** uses `@Test`, `@BeforeEach`, `@AfterEach`.
- **Hibernate** uses `@Entity`, `@Table`, `@Column`.

## Summary Table

| Concept | Usage |
| --- | --- |
| `@interface` | Declares a custom annotation |
| `@Retention` | Defines how long the annotation is kept |
| `@Target` | Defines where the annotation can be applied |
| Reflection | Used to read annotations at runtime |


---

Original Source: https://www.mindstick.com/interview/34069/working-with-annotations-in-java

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
