What Is Annotation

This section describes the annotation programming facility that allows you to associates additional information with a program construct.

What Is Annotation? - Annotation is a programming facility that allows you to associates additional information with a program construct like class, method, variable, etc.

Information provided by an annotation has no impact on the behavior of the program construct. But Java compiler and other tools can make use of the annotated information.

Annotation facility is designed in Java with 4 components:

1. Annotation Type - A special kind of interface type that defined by a "@interface" declaration statement (also called annotation declaration statement).

2. Annotation Declaration - A "@interface" declaration statement that defines an annotation type with or without annotation elements to capture annotated information.

3. Annotation Element - An special method with no parameters declared in an annotation type to capture a piece of annotated information.

4. Annotation Invocation - A "@annotation_type_name(annotated_information)" statement to invoke a given annotation type with specified annotated information. Annotation invocation statements can only be placed immediately before declaration statements of targeted program constructs.

Here is an example of "enum" declaration statement:

/* Header.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */

// Annotation declaration
@interface Header {
   String usage();    // Usage of the targeted construct
}

Now we can use this annotation type to associate information with different types of program constructs using annotation invocation statements.

/* HeaderTest.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */

// Annotation invocation targeting a static variable
@Header(usage="Testing the @Header annotation.")
class HeaderTest {

   // Annotation invocation targeting a static variable
   @Header(usage="To whom you want to greet.")
   private static String name = "Herong";

   // Annotation invocation targeting a method
   @Header(usage="The execution entry point.")
   public static void main(String[] arg) {

      // Annotation invocation targeting a local variable
      @Header(usage="The execution entry point.")
      String message = "How are you?";

      System.out.println(name+", "+message);
   }
}

If you compile and run HeaderTest.java, you will get:

herong> javac Header.java

herong> javac HeaderTest.java

herong> java Header
Herong, How are you?

As you can see, those annotation invocation statements have no impact on the compilation and execution.

So annotations are useless from the execution point of view. But they do offer you a standard way of adding structured comments to your Java program.

Table of Contents

 About This Book

 JDK - Java Development Kit

 Execution Process, Entry Point, Input and Output

 Primitive Data Types and Literals

 Control Flow Statements

 Bits, Bytes, Bitwise and Shift Operations

 Managing Bit Strings in Byte Arrays

 Reference Data Types and Variables

 Enum Types and Enum Constants

 StringBuffer - The String Buffer Class

 System Properties and Runtime Object Methods

 Generic Classes and Parameterized Types

 Generic Methods and Type Inference

 Lambda Expressions and Method References

 Java Modules - Java Package Aggregation

 Execution Threads and Multi-Threading Java Programs

 ThreadGroup Class and "system" ThreadGroup Tree

 Synchronization Technique and Synchronized Code Blocks

 Deadlock Condition Example Programs

 Garbage Collection and the gc() Method

 Assert Statements and -ea" Option

Annotation Statements and Declarations

What Is Annotation

 Use "interface" as Annotation

 Default Values for Annotation Elements

 Single-Element Annotation Invocation

 No-Element (Marker) Annotation Invocation

 getAnnotations() Method - Annotation APIs

 Predefined Annotation Types

 Java Related Terminologies

 Archived Tutorials

 References

 Full Version in PDF/EPUB