Annotations are metadata, used to provide data about programs. Annotations can be used on different declarations such as classes, methods, variables, etc. Annotations can be used for following operations in Java,

  • As an informer to compiler. Example : To suppress errors and warnings.
  • For defining configurations. Example :  To specify configuration like @Controller, etc.
  • To provide run time instructions.

annotations in java

Annotation is defined with at sign (@). The @ symbol indicates that whatever follows the symbol is an annotation. An annotation in it’s simplest form is shown below.


Annotation can include with elements, like name, date,etc as shown below. Elements are separated with “,”. Multiple annotations can be used with single declaration.

@Author(name = "ASB", date = "11/11/2018")

Predefined annotations : Few annotation types are predefined in the Java SE API, which are used in java.lang package. Examples of predefined annotations are @Deprecated, @SuppressWarnings and @Override.

@Deprecated : This annotation is used to indicate that the element is deprecated and should no longer be used. Example usage of deprecated annotation.

static void deprecatedMethod() { }

@Override : Declaring a method with this annotation tells the compiler that this method is overriding the super-class method. Example usage of override annotation.

public void overriddenMethod() { }

@SuppressWarnings : This annotation tells the compiler to ignore particular warnings during compilation. Following is an example of suppressing the unchecked warning.


@SafeVarargs : This annotation is applied to a method or constructor. This annotation asserts that the body of the method or constructor does not perform unsafe activities on its varargs parameters.

@FunctionalInterface : This annotation is used to indicate that the interface is a functional interface. A functional interface is an interface, which contains exactly one abstract method. 

Meta Annotations : These are the annotations defined in java.lang.annotation package. These annotations are used to annotate other annotation types. Few of the meta annotations are given below.

@Retention : This annotation is used to specify how long the annotated type needs to be retained. Following are the retention policies available: 

  • RetentionPolicy.SOURCE : Marked annotation is retained only in source level and is ignored at compiler time.
  • RetentionPolicy.CLASS : Annotation is retained at compile time by compiler and ignored by Java Virtual Machine.
  • RetentionPolicy.RUNTIME : Annotation is retained by the
    Java Virtual Machine and it is used at run time.

@Documented : This annotation is used to indicate the elements, which should be considered by the java doc tool.

@Target : This is used to specify the context(constructor, method, etc.) in which the annotation type can be used.

@Inherited : This annotation is used to indicate that, annotation type can be inherited from the super class.

@Repeatable : This annotation is used to specify that, the annotation can be applied multiple times to the same declaration or type use.

Type Annotations : With Java SE 8 onward, annotations can be used for types along with declarations. An example is : @NonNull String text; Here annotation ensures that the String type text should not be assigned with null.

You may be interested in :