Custom Annotations in Java


Annotations can be declared to include elements that act like methods. This feature allows annotations to carry additional information accessible at runtime, enhancing their utility for tasks like configuration, parsing, or validation.

Source Code

import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface MyCustomAnnotation {
    String description() default "No description";
}

class UseAnnotation {
    @MyCustomAnnotation(description = "This is a custom annotation")
    public void myMethod() {}
}
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments