11.3.2 Annotation Usage Explained
Annotation usage in Java SE 11 involves applying annotations to various elements of your code to provide additional metadata. This metadata can be used by the compiler, runtime, or other tools to enhance the functionality and maintainability of your Java applications.
Key Concepts
1. Applying Annotations
Annotations can be applied to classes, methods, fields, parameters, and other elements. The placement of an annotation determines its scope and the element it affects.
Example
@MyAnnotation public class MyClass { @MyAnnotation private String myField; @MyAnnotation public void myMethod(@MyAnnotation String param) { // Method implementation } }
2. Annotation Parameters
Annotations can have parameters that provide additional information. These parameters can be of various types, such as String
, int
, Class
, or arrays. Parameters can also have default values.
Example
@interface MyAnnotation { String value() default "Default Value"; int count() default 1; } @MyAnnotation(value = "Custom Value", count = 5) public void myMethod() { // Method implementation }
3. Annotation Inheritance
Annotations can be inherited from superclasses. When a class is annotated, its subclasses can inherit those annotations if the annotation type is marked with the @Inherited
meta-annotation.
Example
@Inherited @interface MyAnnotation { String value(); } @MyAnnotation("Parent Class") class Parent { // Class implementation } class Child extends Parent { // Inherits the @MyAnnotation from Parent }
4. Annotation Processing
Annotations can be processed at compile time or runtime. Compile-time processing involves using annotation processors to generate additional source files or perform validation. Runtime processing involves using reflection to access annotations and modify behavior.
Example
@Retention(RetentionPolicy.RUNTIME) @interface MyAnnotation { String value(); } @MyAnnotation("Runtime Value") public class MyClass { public static void main(String[] args) { MyClass obj = new MyClass(); MyAnnotation annotation = obj.getClass().getAnnotation(MyAnnotation.class); System.out.println(annotation.value()); // Output: Runtime Value } }
5. Custom Annotations
Custom annotations can be created to serve specific purposes in 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) @interface Loggable { String value() default "INFO"; } public class MyClass { @Loggable("DEBUG") public void myMethod() { // Method implementation } }
Examples and Analogies
Think of annotations as sticky notes attached to your code. These notes provide additional information to the compiler, tools, or runtime environment. For example, the @Override
annotation is like a sticky 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 messages. For instance, the @Loggable
annotation could be a note that says, "Log the result of this method with a specific log level."
By mastering annotation usage, you can enhance the readability, maintainability, and functionality of your Java SE 11 applications, making your code more expressive and easier to manage.