Ravi Vishwakarma is a dedicated Software Developer with a passion for crafting efficient and innovative solutions. With a keen eye for detail and years of experience, he excels in developing robust software systems that meet client needs. His expertise spans across multiple programming languages and technologies, making him a valuable asset in any software development project.
Ravi Vishwakarma
24-Apr-2025In 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
This defines an annotation named
MyAnnotation
that has a single required element calledvalue
.2. Adding Metadata:
@Target
and@Retention
You should define where the annotation can be applied and how long it should be retained.
@Retention(RetentionPolicy.RUNTIME)
— Makes the annotation available during runtime.@Target(ElementType.METHOD)
— Limits annotation use to methods.3. Using the Custom Annotation
4. Accessing Custom Annotations Using Reflection
5. Common
@Target
ValuesElementType
TYPE
FIELD
METHOD
PARAMETER
CONSTRUCTOR
LOCAL_VARIABLE
Summary
@interface
to define your annotation.@Retention
to specify when the annotation is available.@Target
to limit where the annotation can be used.