Java Meta Annotations

Java Meta Annotations

Summary: As the name indicate Java Meta Annotations annotate another annotation. Confused. No, start reading to be clear. Explained in simple terms, example codes etc.

Meta Annotations (Annotations applied to other annotations, or to say, annotations-of-annotations)

After understanding what an annotation is and seeing language built-in standard annotations let us go for other predefined annotations – meta annotations.

Java Meta Annotations annotate an annotation. That is, annotation for another annotation. Following is the list.

1. @Retention
2. @Target
3. @Documented
4. @Inherited

Let us go in detail of each.

1. @Retention

The @Retention annotation describes the retention (availability or preservation) policy of the annotation. Retention policy specifies to what extent the annotation should be made available – in source code only or class file or runtime.

Following are the possible values for @Retention:

1. RetentionPolicy.SOURCE: Annotation is available in source code only but discarded while compilation.

2. RetentionPolicy.CLASS: Annotation exists in the .class file generated but discarded at runtime. As the annotation is not available at runtime, runtime cannot make use of the annotation.

3. RetentionPolicy.RUNTIME: Annotation is available for both compiler and runtime also. That is, annotation is stored in .class file and also runtime can make use of. It is the most used.

2. @Target

The @Target annotation specifies for what elements of the class the annotation is to be applied. Following are the possible types

1. @Target(ElementType.TYPE): If the value is TYPE, the annotation can be applied to any element of the class. It is the default also.
2. @Target(ElementType.FIELD): Applicable to fields (instance variables) of a class.
3. @Target(ElementType.METHOD): Applicable to methods of a class.
4. @Target(ElementType.PARAMETER): Applicable to parameters of the method.
5. @Target(ElementType.CONSTRUCTOR): Applicable to a constructors.
6. @Target(ElementType.LOCAL_VARIABLE): Applicable to a local variables.
7. @Target(ElementType.ANNOTATION_TYPE):Says that declared type by itself is an annotation type

3. @Documented

The elements annotated with @Documented should be documented using JavaDoc tool. This annotation instructs JavaDoc tool that the annotation should be visible in Java documentation for all the classes using the custom annotation.

4. @Inherited

The element annotated with @Inherited indicates that the annotation can be inherited from any of the super classes. The compiler queries all the super classes one-by-one until the required annotation is found. This annotation is applicable to classes only.

In the super class, if a method is annotated, the subclass overridden is not annotated. That is, annotations are not inherited to subclasses. If declared @Inherited, the subclass gets the annotation properties inherited from the super class.

These Java Meta annotations can be explained in user-defined or custom-defined annotations. Now let us learn how to write our own annotations in the next tutorials.

8 thoughts on “Java Meta Annotations”

  1. I am curious to find out what blog platform you’re
    using? I’m experiencing some minor security problems with my latest website and
    I would like to find something more risk-free.
    Do you have any solutions?

Leave a Comment

Your email address will not be published.