Understanding Java Annotations: Benefits, Types, Custom Annotations and Runtime Parsing
This article explains the purpose and advantages of Java annotations, introduces common JDK and third‑party annotations, classifies them by lifecycle and source, shows how to define custom annotations with meta‑annotations, and demonstrates parsing them at runtime using reflection with concrete code examples.
Annotations are widely used in Java development to make code more readable, reduce configuration files, and allow developers to create custom solutions that stand out in interviews.
Benefits of annotations: they help understand third‑party framework code, replace verbose configuration with concise metadata, and showcase advanced skills when custom annotations are used.
Common JDK annotations such as @Override , @Deprecated and @SuppressWarnings are demonstrated with simple interface and class examples:
public interface people {
public String name();
public int age();
public void work();
}
public class Child implements people {
@Override
public String name() { return null; }
@Override
public int age() { return 0; }
@Override
public void work() { }
}When a method is removed from the interface, the @Override annotation causes a compile‑time error, illustrating its safety role.
Other JDK annotations like @Deprecated and @SuppressWarnings("deprecation") are shown in a test class to suppress warnings for outdated methods.
public class Test {
@SuppressWarnings("deprecation")
public void work() {
people p = new Child();
p.work();
}
}Third‑party annotations (e.g., Spring’s @Autowired ) are mentioned as runtime annotations that affect program behavior.
Classification of annotations is presented by lifecycle (source, class, runtime) and origin (JDK, third‑party, custom), as well as the concept of meta‑annotations.
Defining a custom annotation includes the required meta‑annotations and member declarations:
@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@Documented
public @interface Description {
String desc();
String author();
int age() default 18;
}Usage of the custom annotation on a method:
@Description(desc="i am Color", author="boy", age=18)
public String Color() { return "red"; }Parsing annotations at runtime with reflection is illustrated by loading a class, checking for the annotation, and printing its values:
public class ParseAnn {
public static void main(String[] args) {
try {
Class c = Class.forName("com.test.Child");
if (c.isAnnotationPresent(Description.class)) {
Description d = (Description) c.getAnnotation(Description.class);
System.out.println(d.value());
}
} catch (ClassNotFoundException e) { e.printStackTrace(); }
}
}Similar reflection code iterates over methods to retrieve method‑level annotations, demonstrating both direct and generic approaches.
The article concludes with a prompt to experiment with different @Retention policies (SOURCE, CLASS, RUNTIME) to see their impact on annotation availability.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.