Javatpoint Logo
Javatpoint Logo

Java Type and Repeating Annotations


Java Type Annotations

Java 8 has included two new features repeating and type annotations in its prior annotations topic. In early Java versions, you can apply annotations only to declarations. After releasing of Java SE 8 , annotations can be applied to any type use. It means that annotations can be used anywhere you use a type. For example, if you want to avoid NullPointerException in your code, you can declare a string variable like this:

Following are the examples of type annotations:

Note - Java created type annotations to support improved analysis of Java programs. It supports way of ensuring stronger type checking.


Java Repeating Annotations

In Java 8 release, Java allows you to repeating annotations in your source code. It is helpful when you want to reuse annotation for the same class. You can repeat an annotation anywhere that you would use a standard annotation.

For compatibility reasons, repeating annotations are stored in a container annotation that is automatically generated by the Java compiler. In order for the compiler to do this, two declarations are required in your code.

  1. Declare a repeatable annotation type
  2. Declare the containing annotation type

1) Declare a repeatable annotation type

Declaring of repeatable annotation type must be marked with the @Repeatable meta-annotation. In the following example, we have defined a custom @Game repeatable annotation type.

The value of the @Repeatable meta-annotation, in parentheses, is the type of the container annotation that the Java compiler generates to store repeating annotations. In the following example, the containing annotation type is Games. So, repeating @Game annotations is stored in an @Games annotation.


2) Declare the containing annotation type

Containing annotation type must have a value element with an array type. The component type of the array type must be the repeatable annotation type. In the following example, we are declaring Games containing annotation type:

Note - Compiler will throw a compile-time error, if you apply the same annotation to a declaration without first declaring it as repeatable.


Java Repeating Annotations Example

OUTPUT:

Cricket on Sunday
Hockey on Friday
Football on Saturday




Please Share

facebook twitter google plus pinterest

B.Tech / MCA


Learn Latest Tutorials