Java Annotation Types: A Comprehensive Guide to Predefined, Custom, and Meta Annotations
Java Annotation Types
This tutorial covers Java annotations—predefined, custom, and meta‑annotations—with clear examples.
Java annotations are metadata that describe elements in your source code. The Java SE platform supplies a set of standard annotations, and developers can create custom ones tailored to their projects. Annotations can also be annotated themselves, forming what are known as meta‑annotations.
Annotations fall into three categories:
- Predefined annotations – built into the Java platform.
- Custom annotations – user‑defined for specific needs.
- Meta‑annotations – annotations applied to other annotations.
Predefined Annotation Types
1. @Deprecated
The @Deprecated marker signals that a class, method, field, or other element is obsolete and has a newer alternative. The compiler issues a warning whenever a deprecated element is used.
Syntax:
@Deprecated
public void oldMethod() { ... }
Javadoc provides the @deprecated tag for documentation:
/**
* @deprecated
* Use {@link #newMethod()} instead.
*/
@Deprecated
public void oldMethod() { ... }
Example
class Main {
/**
* @deprecated
* This method is deprecated and has been replaced by newMethod()
*/
@Deprecated
public static void deprecatedMethod() {
System.out.println("Deprecated method");
}
public static void main(String[] args) {
deprecatedMethod();
}
}
Output
Deprecated method
2. @Override
The @Override annotation indicates that a method is intended to override a method from a superclass or interface. While not mandatory, it catches errors such as mismatched signatures at compile time.
Example
class Animal {
public void display() {
System.out.println("I am an animal");
}
}
class Dog extends Animal {
@Override
public void display() {
System.out.println("I am a dog");
}
public void printMessage() {
display();
}
}
class Main {
public static void main(String[] args) {
Dog dog1 = new Dog();
dog1.printMessage();
}
}
Output
I am a dog
3. @SuppressWarnings
This annotation instructs the compiler to ignore specific warning categories. Common categories include deprecation and unchecked.
Usage:
@SuppressWarnings("deprecation")
Multiple categories:
@SuppressWarnings({"deprecation", "unchecked"})
Example
class Main {
@Deprecated
public static void deprecatedMethod() {
System.out.println("Deprecated method");
}
@SuppressWarnings("deprecation")
public static void main(String[] args) {
Main depObj = new Main();
depObj.deprecatedMethod();
}
}
Output
Deprecated method
4. @SafeVarargs
Applied to final, static, or private methods and constructors, @SafeVarargs asserts that the method does not perform unsafe operations on its varargs parameter. This removes unchecked warnings related to varargs.
Example
import java.util.*;
class Main {
@SafeVarargs
private static void displayList(List... lists) {
for (List list : lists) {
System.out.println(list);
}
}
public static void main(String[] args) {
List universityList = Arrays.asList("Tribhuvan University", "Kathmandu University");
List programmingLanguages = Arrays.asList("Java", "C");
displayList(universityList, programmingLanguages);
}
}
Output
[Tribhuvan University, Kathmandu University] [Java, C]
5. @FunctionalInterface
Introduced in Java 8, this annotation designates an interface as a functional interface—an interface with a single abstract method. The compiler verifies this constraint.
Example
@FunctionalInterface
public interface MyFuncInterface {
void firstMethod(); // abstract
}
Adding a second abstract method triggers a compilation error:
@FunctionalInterface
public interface MyFuncInterface {
void firstMethod();
void secondMethod(); // compile‑time error
}
Multiple default or static methods are allowed.
@FunctionalInterface
public interface MyFuncInterface {
void firstMethod();
default void secondMethod() { /* ... */ }
default void thirdMethod() { /* ... */ }
}
Custom Annotations
Developers can create annotations that suit application‑specific requirements. The syntax uses @interface and can include element methods with optional default values.
Syntax
@interface AnnotationName {
ElementType elementName() default defaultValue;
}
Example
@interface MyCustomAnnotation {
String value() default "default value";
}
class Main {
@MyCustomAnnotation(value = "programiz")
public void method1() {
System.out.println("Test method 1");
}
public static void main(String[] args) {
new Main().method1();
}
}
Output
Test method 1
Meta Annotations
Meta‑annotations configure how other annotations behave.
1. @Retention
Defines the scope of an annotation’s visibility: SOURCE, CLASS, or RUNTIME.
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation { }
2. @Documented
Ensures the annotation appears in generated Javadoc.
@Documented
public @interface MyAnnotation { }
3. @Target
Restricts the annotation to specific program elements.
@Target(ElementType.METHOD)
public @interface MyAnnotation { }
Element types include ANNOTATION_TYPE, CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, TYPE.
4. @Inherited
Allows a subclass to inherit an annotation present on a superclass.
@Inherited
public @interface MyAnnotation { }
5. @Repeatable
Permits an annotation to be applied multiple times to the same element. Requires a container annotation.
@Repeatable(Universities.class)
public @interface University {
String name();
}
public @interface Universities {
University[] value();
}
Usage:
@University(name = "TU")
@University(name = "KU")
private String uniName;
To access annotation data at runtime, use the Reflection API’s getAnnotationsByType() or getAnnotations() methods.
Java
- Java Primitive Data Types: A Complete Guide with Examples
- Java Methods: How to Define, Call, and Use Them Effectively
- Java Recursion: Understanding, Examples, and Trade‑Offs
- Mastering Method Overriding in Java
- Mastering Java Polymorphism: Concepts, Examples, and Best Practices
- Mastering Java's Iterator Interface: Practical Guide with Code Example
- Understanding Java Variable Types: A Comprehensive Guide
- Mastering Java Methods: Create, Invoke, and Abstraction
- Java Method Overriding: Customizing Superclass Behavior
- Mastering Java 8: A Comprehensive Guide to Method References