Subjects

All subjects Django Java Python React Spring Boot JavaScript PHP
Sign Up Free
Interview question

How do you create a custom annotation in Java? Java में custom annotation कैसे बनाएं?

Answer

Custom annotations are defined using @interface, combined with meta-annotations (@Retention, @Target) that control where the annotation can be used and how long it's retained.

import java.lang.annotation.*;

// Defining a custom annotation
@Retention(RetentionPolicy.RUNTIME)  // available at runtime via reflection
@Target(ElementType.METHOD)           // can only be applied to methods
public @interface Testable {
    String author() default 'Unknown';  // element with a default value
    int priority() default 1;
    String[] tags() default {};
}

// Using the custom annotation
class Calculator {
    @Testable(author = 'John', priority = 5, tags = {'math', 'critical'})
    public int add(int a, int b) {
        return a + b;
    }

    @Testable  // uses default values
    public int subtract(int a, int b) {
        return a - b;
    }
}

// Reading the annotation via reflection at runtime
import java.lang.reflect.Method;

public class AnnotationProcessor {
    public static void main(String[] args) throws Exception {
        Method method = Calculator.class.getMethod('add', int.class, int.class);
        if (method.isAnnotationPresent(Testable.class)) {
            Testable annotation = method.getAnnotation(Testable.class);
            System.out.println('Author: ' + annotation.author());       // John
            System.out.println('Priority: ' + annotation.priority());   // 5
        }
    }
}

// RetentionPolicy options:
// SOURCE - discarded by the compiler, not in bytecode (e.g. @Override)
// CLASS - kept in bytecode but not available at runtime (default)
// RUNTIME - available via reflection at runtime (needed for frameworks)

// ElementType options: TYPE, METHOD, FIELD, PARAMETER, CONSTRUCTOR, etc.

Custom annotations @interface से define होते हैं, meta-annotations (@Retention, @Target) के साथ जो control करते हैं कि annotation कहां use हो सकता है और कितनी देर तक retain होता है।

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Testable {
    String author() default 'Unknown';
    int priority() default 1;
    String[] tags() default {};
}

class Calculator {
    @Testable(author = 'John', priority = 5, tags = {'math', 'critical'})
    public int add(int a, int b) {
        return a + b;
    }

    @Testable
    public int subtract(int a, int b) {
        return a - b;
    }
}

import java.lang.reflect.Method;

public class AnnotationProcessor {
    public static void main(String[] args) throws Exception {
        Method method = Calculator.class.getMethod('add', int.class, int.class);
        if (method.isAnnotationPresent(Testable.class)) {
            Testable annotation = method.getAnnotation(Testable.class);
            System.out.println('Author: ' + annotation.author());
            System.out.println('Priority: ' + annotation.priority());
        }
    }
}

// RetentionPolicy: SOURCE, CLASS, RUNTIME
// ElementType: TYPE, METHOD, FIELD, PARAMETER, CONSTRUCTOR

Was this answer clear?