Return to site

JAVA ANNOTATIONS: @Target

· java

Define

@Target is an annotation that applies to other annotations. Annotations that apply to other annotations are called meta-annotations. There are several meta-annotation types defined in java.lang.annotation.

@Target annotation marks another annotation to restrict what kind of Java elements the annotation can be applied to.

A target annotation specifies one of the following element types as its value:

  • ElementType.ANNOTATION_TYPE can be applied to an annotation type. 
  • ElementType.CONSTRUCTOR can be applied to a constructor. 
  • ElementType.FIELD can be applied to a field or property. 
  • ElementType.LOCAL_VARIABLE can be applied to a local variable. 
  • ElementType.METHOD can be applied to a method-level annotation. 
  • ElementType.PACKAGE can be applied to a package declaration. 
  • ElementType.PARAMETER can be applied to the parameters of a method. 
  • ElementType.TYPE can be applied to any element of a class.

With JDK11 there are also:

  • ElementType.MODULE 
  • ElementType.TYPE_PARAMETER

Snippet