69

java.lang.annotation.ElementType:

A program element type. The constants of this enumerated type provide a simple classification of the declared elements in a Java program. These constants are used with the Target meta-annotation type to specify where it is legal to use an annotation type.

There are the following constants:

  • ANNOTATION_TYPE - Annotation type declaration
  • CONSTRUCTOR - Constructor declaration
  • FIELD - Field declaration (includes enum constants)
  • LOCAL_VARIABLE - Local variable declaration
  • METHOD - Method declaration
  • PACKAGE - Package declaration
  • PARAMETER - Parameter declaration
  • TYPE - Class, interface (including annotation type), or enum declaration

Can someone explain what each of them are (where they'd be annotated in actual code)?

3
  • Aren't the descriptions clear enough? Surely you understand at least some of them?
    – meriton
    Commented Aug 23, 2010 at 18:03
  • @meriton: I've only seen annotations on fields, methods, and classes, which probably corresponds with FIELD, METHOD, and TYPE. I'll post examples of annotations for each of these, and perhaps someone will fill out the rest? Commented Aug 23, 2010 at 18:06
  • Java 8 adds two new constants, TYPE_PARAMETER and TYPE_USE.
    – mernst
    Commented Mar 5, 2015 at 18:40

3 Answers 3

127

Let's say the annotation to which you specify the ElementType is called YourAnnotation:

  • ANNOTATION_TYPE - Annotation type declaration. Note: This goes on other annotations

    @YourAnnotation
    public @interface AnotherAnnotation {..}
    
  • CONSTRUCTOR - Constructor declaration

    public class SomeClass {
        @YourAnnotation
        public SomeClass() {..}
    }
    
  • FIELD - Field declaration (includes enum constants)

    @YourAnnotation
    private String someField;
    
  • LOCAL_VARIABLE - Local variable declaration. Note: This can't be read at runtime, so it is used only for compile-time things, like the @SuppressWarnings annotation.

    public void someMethod() {
        @YourAnnotation int a = 0;
    }
    
  • METHOD - Method declaration

    @YourAnnotation
    public void someMethod() {..}
    
  • PACKAGE - Package declaration. Note: This can be used only in package-info.java.

    @YourAnnotation
    package org.yourcompany.somepackage;
    
  • PARAMETER - Parameter declaration

    public void someMethod(@YourAnnotation param) {..}
    
  • TYPE - Class, interface (including annotation type), or enum declaration

    @YourAnnotation
    public class SomeClass {..}
    

You can specify multiple ElementTypes for a given annotation. E.g.:

@Target({ElementType.CONSTRUCTOR, ElementType.METHOD})
1
  • 2
    @Bozho: Thanks for the pro tip. I check-marked the thing below the vote counter. Commented Aug 23, 2010 at 18:17
65

This summarizes the main ones:

@CustomTypeAnnotation
public class MyAnnotatedClass {
  @CustomFieldAnnotation
  private String foo;

  @CustomConstructorAnnotation
  public MyAnnotatedClass() {
  }

  @CustomMethodAnnotation
  public String bar(@CustomParameterAnnotation String str) {
    @CustomLocalVariableAnnotation String asdf = "asdf";
    return asdf + str;
  }
}

ANNOTATION_TYPE is an annotation on another annotation, like this:

@CustomAnnotationTypeAnnotation
public @interface SomeAnnotation {
  ..
}

Package is defined in a package-info.java file in the package, like this:

@CustomPackageLevelAnnotation
package com.some.package;

import com.some.package.annotation.PackageLevelAnnotation;

For more info on PACKAGE annotations see here and here.

2
  • 4
    Answer would be that much better if you also included snippets for ANNOTATION_TYPE and for PACKAGE. Commented Jun 17, 2014 at 18:03
  • 3
    @JohnGlassmyer - A few years late, but I added more snippets for ANNOTATION_TYPE and PACKAGE. :-) Commented Mar 29, 2018 at 5:30
4

TYPE:

Annotation:

@Target({ElementType.TYPE})    // This annotation can only be applied to
public @interface Tweezable {  // class, interface, or enum declarations.
}

and an example usage:

@Tweezable
public class Hair {
    ...
}
1
  • OK, but what value does this add to your program? What does it indicate to the interpreter, what does it do, etc? What is the interpreter going to do with the information that this class is "@Tweezable"? Java already has an interface keyword, you can simply write "public interface Tweezable". What, if anything, does the annotation do, and does it duplicate the functionality of just declaring an interface the straightforward way? This is the kind of question I came here hoping to find the answers to.
    – Sean
    Commented Apr 13, 2022 at 21:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.