Java Built-In Annotations

In this chapter you will learn:

  1. What are built-in annotations
  2. @Retention
  3. @Documented
  4. @Target
  5. @Inherited
  6. @Override
  7. @Deprecated
  8. @SuppressWarnings

Built-In Annotations

Java defines many built-in annotations. Most are specialized, but seven are general purpose.

  • @Retention
  • @Documented
  • @Target
  • @Inherited
  • @Override
  • @Deprecated
  • @SuppressWarnings

@Retention

@Retention is designed to be used only as an annotation to another annotation. It specifies the retention policy.

@Documented

The @Documented annotation is a marker interface that tells a tool that an annotation is to be documented.

@Target

The @Target annotation specifies the types of declarations to which an annotation can be applied.

It is designed to be used only as an annotation to another annotation. @Target takes one argument, which must be a constant from the ElementType enumeration.

Target constant annotation can be applied to

  • ANNOTATION_TYPE Another annotation
  • CONSTRUCTOR Constructor
  • FIELD Field
  • LOCAL_VARIABLE Local variable
  • METHOD Method
  • PACKAGE Package
  • PARAMETER Parameter
  • TYPE Class, interface, or enumeration

You can specify one or more of these values in a @Target annotation. To specify multiple values, you must specify them within a braces-delimited list. For example:


@Target( { ElementType.FIELD, ElementType.LOCAL_VARIABLE } )

@Inherited

@Inherited is a marker annotation that can be used only on another annotation declaration. It affects only annotations that will be used on class declarations. @Inherited causes the annotation for a superclass to be inherited by a subclass.

@Override

@Override is a marker annotation that can be used only on methods. A method annotated with @Override must override a method from a superclass.

@Deprecated

@Deprecated is a marker annotation. It indicates that a declaration is obsolete and has been replaced by a newer form.

@SuppressWarnings

@SuppressWarnings specifies that one or more warnings that might be issued by the compiler are to be suppressed. The warnings to suppress are specified by name, in string form. This annotation can be applied to any type of declaration.

Next chapter...

What you will learn in the next chapter:

  1. What is a generic type
  2. What is the syntax for declaring a generic class
  3. Example - Java Generic Type
  4. A Generic Class with Two Type Parameters
  5. Example - Generic Queue
Home »
  Java Tutorial »
    Java Langauge »
      Java Annotations
Java Annotations
Java Annotation retention policy
Java Annotation reflection
Java Annotation Default Values
Java Marker annotation
Java Single-Member Annotations
Java Built-In Annotations