No-Element (Marker) Annotation Invokation

This section provides a tutorial example on how to use the marker format in annotation invokation statements, if no annotation elements are needed.

If an annotation invokation statement has no annotation elements, you can omit the empty parentheses, ().

An annotation invokation statement with no annotation elements is called "Marker Annotation", because it appears like a marker in front of the targetted program construct.

Here is a sample program that shows you how to use the marker format in annotation invokation statements, if no annotation elements are needed.

/* MarkerAnnotation.java
 * Copyright (c) HerongYang.com. All Rights Reserved.
 */
class MarkerAnnotation {

   // Annotation declarations
   @interface Herong {
      String value() default "Herong";
      String email() default "herong@nowhere.com";
   }
   @interface John {
      String value() default "John";
      String email() default "john@nowhere.com";
   }

   // Annotation invokation - Marker format
   @Herong public static void main(String[] arg) {
      printMsg("Hello world!");
   }

   // Annotation invokation - Normal format
   @John() public static void printMsg(String msg) {
      System.out.println(msg);
   }
}

If you compile and run the program, you will get:

herong> java MarkerAnnotation.java

Hello world!

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 Invokation

No-Element (Marker) Annotation Invokation

 getAnnotations() Method - Annotation APIs

 Predefined Annotation Types

 Java Related Terminologies

 Outdated Tutorials

 References

 Full Version in PDF/EPUB