Java - Annotation Member Array Type

Introduction

An annotation can have elements of an array type. The array type could be of one of the following types:

  • A primitive type
  • java.lang.String type
  • java.lang.Class type
  • An enum type
  • An annotation type

You need to specify the value for an array element inside braces.

Elements of the array are separated by a comma.

@interface ToDo { 
        String[] items(); 
} 

The following code shows how to use a @ToDo annotation:

@ToDo(items={"a", "b"}) 
class Test { 
} 
  

To use only one element in the array, omit the braces. The following two annotation instances of the ToDo annotation type are equivalent:

  
@ToDo(items={"a"}) 
@ToDo(items="b")  

To use empty array:

@ToDo(items={})

No Null Value

You cannot use a null reference as a value for an element in an annotation.