As the name suggests, Repeating Annotations imply that a particular annotation is applied multiple times to a declaration.
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.
@Repeatable(Colors.class)
@interface Color {
String name();
@Retention(RetentionPolicy.RUNTIME)
}
Color[] value();
@interface Colors {
}
@Color(name = "red")
@Color(name = "blue")
@Color(name = "green")
class Shirt {
}
public class RepeatingAnnotations {
public static void main(String args[]) {
Color[] colorArray = Shirt.class.getAnnotationsByType(Color.class);
for (Color color : colorArray) {
System.out.println(color.name());
}
}
}