11.1 Annotation Basics Explained
Annotations in Java SE 11 are a form of metadata that provide information about the code. They do not directly affect the execution of the code but are used by the compiler, tools, or frameworks to generate additional code, configure behavior, or enforce rules. Understanding annotation basics is essential for leveraging the full power of Java SE 11.
Key Concepts
1. What is an Annotation?
An annotation is a special form of syntactic metadata that can be added to Java source code. Classes, methods, variables, parameters, and packages can be annotated. Annotations are defined using the @interface
keyword.
Example
public @interface MyAnnotation { String value(); }
2. Built-in Annotations
Java SE 11 provides several built-in annotations, such as @Override
, @Deprecated
, and @SuppressWarnings
. These annotations are used to indicate specific behaviors or to suppress warnings.
Example
@Override public String toString() { return "MyObject"; }
3. Custom Annotations
Custom annotations can be created to provide specific metadata for your application. Custom annotations are defined using the @interface
keyword and can have elements that define their behavior.
Example
@Retention(RetentionPolicy.RUNTIME) @Target(ElementType.METHOD) public @interface Loggable { String value() default "INFO"; }
4. Annotation Elements
Annotations can have elements that define their behavior. These elements can be of various types, such as String
, int
, Class
, or arrays. Elements can also have default values.
Example
public @interface MyAnnotation { String name(); int age() default 18; }
5. Annotation Retention Policies
The retention policy of an annotation determines where the annotation is available. The three retention policies are SOURCE
, CLASS
, and RUNTIME
. The default retention policy is CLASS
.
Example
@Retention(RetentionPolicy.RUNTIME) public @interface MyAnnotation { String value(); }
6. Annotation Targets
The target of an annotation specifies the types of elements to which the annotation can be applied. The targets include TYPE
, METHOD
, FIELD
, PARAMETER
, and others.
Example
@Target(ElementType.METHOD) public @interface MyAnnotation { String value(); }
Examples and Analogies
Think of annotations as sticky notes that you attach to your code. These notes provide additional information to the compiler, tools, or frameworks. For example, the @Override
annotation is like a note that says, "This method is intended to override a method from a superclass."
Custom annotations are like creating your own sticky notes with specific instructions. For example, the @Loggable
annotation could be a note that says, "Log the result of this method with a specific log level."
Retention policies determine when the sticky notes are visible. If the policy is SOURCE
, the note is only visible during the initial writing of the code. If the policy is RUNTIME
, the note is visible even when the code is running.
By mastering annotation basics, you can enhance your Java SE 11 applications with powerful metadata that improves code readability, maintainability, and functionality.