Meta Annotation – annotate other annotation

Author posted by Jitendra on Posted on under category Categories JAVA and tagged as Tags with 1 Comment on Meta Annotation – annotate other annotation

What is Meta Annotation and its usage in JAVA

In Previous article, we have seen that what are the types of annotation provided by the JAVA.

What is Meta Annotation ?

Fortunately Java allows us to define our own annotation. The annotations which are used to define costume annotations are known as “Meta Annotation“.

There are four annotation types in the “java.lang.annotation” package. These so-called meta-annotations are used to annotate other annotation types.

Inbuilt Annotation and Meta Annotations in JAVA
Inbuilt Annotation and Meta Annotations in JAVA

1. @Documented:
If the Annotation itself is annotated by @Documented, then in the java documentation, annotation will also be shown with the description of class and members.

For Example:

package com.G2.Annotations.Meta;

import java.lang.annotation.Documented;

@interface Nagpur{}

@interface Mumbai{}

public class TestDocumented {


In above code, when the documentation for the class “TestDocumented” is generated, @Nagpur will be shown in the document whereas Mumbai will not.

2. @ Inherited:
If any Annotation is marked by @Inherited then the child class inherits that annotation.
Example :

package com.G2.Annotations.Meta;

import java.lang.annotation.Inherited;

@interface Maharashtra {

@interface Gujarat {

class Gujrat_Maharashtra {


class Nag extends Gujrat_Maharashtra {
	public void display() {
		System.out.println("Nagpur is in Maharashtra");

In above example, there are two annotations defined: “@Maharashtra” and “@Gujrat”.
Subclass “Nag” inherited annotation “@Maharashtra” only from its parent class “Gujarat_Maharashtra”, because it is marked as @Inherited.

3. @Retention
Different annotation types have different purposes. Some are intended for use with the compiler; others are meant to be reflected dynamically at runtime. There’s no reason for a compiler annotation to be available at runtime.
This Meta annotation – @Retention is responsible to specify that how long an annotation type should be retained.

The value attribute is one of the “java.lang.annotation.Retention” Policy enum constants. The possible values, in order from shortest to longest retention, are as follows:

The annotation will not be included in the class file. This is useful for annotations which are intended for the compiler only.

The annotation will be included in the class file, but cannot be read reflectively.

The annotation can be reflected at runtime.

If no @Retention policy is specified, it defaults to RetentionPolicy.CLASS.

4. @Target

@Target annotation decided which type of elements can be marked by the annotation.

If the @Target is method and annotation is applied on any other element lets say class, then compiler will generate an error.

The value attribute is one or more of the “java.lang.annotation.ElementType” enum constants. Those constants are ElementType.ANNOTATION_TYPE, CONSTRUCTOR, FIELD, LOCAL_VARIABLE, METHOD, PACKAGE, PARAMETER, and TYPE.

Related posts

One thought on “Meta Annotation – annotate other annotation”

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.