11 Java Annotations Explained
Java annotations are a form of metadata that provide information about the code without changing its behavior. They are used to convey additional information to the compiler, tools, or runtime environments. Understanding these annotations is crucial for developing robust and maintainable Java SE 11 applications.
Key Concepts
1. @Override
The @Override annotation is used to indicate that a method is intended to override a method in a superclass. It helps the compiler verify that the method signature matches the one in the superclass.
Example
class Parent {
void show() {
System.out.println("Parent's show()");
}
}
class Child extends Parent {
@Override
void show() {
System.out.println("Child's show()");
}
}
2. @Deprecated
The @Deprecated annotation marks a method, class, or field as deprecated. It indicates that the element should no longer be used and may be removed in future versions.
Example
@Deprecated
public void oldMethod() {
System.out.println("This method is deprecated.");
}
3. @SuppressWarnings
The @SuppressWarnings annotation is used to suppress compiler warnings for a specific block of code. It helps in managing warnings that are deemed safe to ignore.
Example
@SuppressWarnings("unchecked")
List<String> list = (List<String>) new ArrayList();
4. @FunctionalInterface
The @FunctionalInterface annotation is used to indicate that an interface is intended to be a functional interface. It ensures that the interface has exactly one abstract method.
Example
@FunctionalInterface
interface MyFunctionalInterface {
void doSomething();
}
5. @SafeVarargs
The @SafeVarargs annotation is used to suppress unchecked warnings related to varargs (variable-length argument lists). It is typically used in methods that handle varargs safely.
Example
@SafeVarargs
public final void display(List<String>... lists) {
for (List<String> list : lists) {
System.out.println(list);
}
}
6. @Retention
The @Retention annotation specifies how long annotations with the annotated type are to be retained. It can be retained at compile time, runtime, or source level.
Example
@Retention(RetentionPolicy.RUNTIME)
@interface MyAnnotation {
String value();
}
7. @Target
The @Target annotation specifies the kinds of program elements to which an annotation type is applicable. It can be applied to classes, methods, fields, etc.
Example
@Target(ElementType.METHOD)
@interface MyMethodAnnotation {
String value();
}
8. @Documented
The @Documented annotation indicates that elements using the annotated type should be documented by JavaDoc and similar tools.
Example
@Documented
@interface MyDocAnnotation {
String value();
}
9. @Inherited
The @Inherited annotation indicates that the annotation type can be inherited from the superclass. This is not true by default.
Example
@Inherited
@interface MyInheritedAnnotation {
String value();
}
10. @Repeatable
The @Repeatable annotation indicates that the annotation type can be applied more than once to the same declaration or type use.
Example
@Repeatable(MyRepeatableAnnotations.class)
@interface MyRepeatableAnnotation {
String value();
}
@interface MyRepeatableAnnotations {
MyRepeatableAnnotation[] value();
}
11. @Native
The @Native annotation is used to indicate that a field is a constant that may be referenced from native code. It is typically used in conjunction with JNI (Java Native Interface).
Example
class NativeExample {
@Native
static final int CONSTANT = 42;
}
Examples and Analogies
Think of annotations as sticky notes attached to your code. Each sticky note (annotation) provides additional information or instructions to the compiler, tools, or runtime environment. For example, the @Override annotation is like a sticky note that says, "This method is supposed to replace a method in the parent class." The @Deprecated annotation is like a sticky note that warns, "This method is old and shouldn't be used anymore."
By mastering these annotations, you can enhance the clarity, maintainability, and functionality of your Java SE 11 applications, ensuring that your code is well-documented and adheres to best practices.