@Inherited annotation indicates that the annotation type can be inherited from the superclass.
(This is not true by default.)
This is an annotation for annotations.
It means that subclasses of annotated classes are considered having the same annotation as their superclass.
@Target is an annotation that applies to other annotations.
Annotations that apply to other annotations are called meta-annotations.
There are several meta-annotation types defined in java.lang.annotation.
@Target annotation marks another annotation to restrict what kind of Java elements the annotation can be applied to.
A target annotation specifies one of the following element types as its value:
With JDK11 there are also:
There are some situations where you want to apply the same annotation to a declaration or type use. As of the Java SE 8 release, repeating annotations enable you to do this.
Set a timer to run a method, doPeriodicCleanup, on the last day of the month, and on every Friday at 11:00 p.m.
―And thanks to
@Override & @SuppressWarnings
@Override annotation informs the compiler that the element is meant to override an element declared in a superclass.
@SuppressWarnings annotation tells the compiler to suppress specific warnings that it would otherwise generate.
Like @SupressWarnings but for varargs parameters.
@SafeVarargs annotation, when applied to a method or constructor, asserts that the code does not perform potentially unsafe operations on its varargs parameter.
When this annotation type is used, unchecked warnings relating to varargs usage are suppressed.
Before Java 9 versions, if vararg methods are used with generics, then there is a warning message. Even though not all methods create heap pollution, compiler shows warning for all vararg methods used with generics. That's the reason @SafeVarargs concept was added to Java 9 version to avoid these warnings.
So use @SafeVarargs annotation on final or static methods and constructors.
@SafeVarargs is like @SupressWarnings in that it allows us to declare that a particular compiler warning is a false positive.
Indicates that the type declaration is intended to be a functional interface.
―An informative annotation type used to indicate that an interface type declaration is intended to be a functional interface as defined by the Java Language Specification. Conceptually, a functional interface has exactly one abstract method.
―Note that instances of functional interfaces can be created with lambda expressions, method references, or constructor references.
―If a type is annotated with this annotation type, compilers are required to generate an error message unless:
@Deprecated annotation indicates that the marked element is deprecated and should no longer be used.
The compiler generates a warning whenever a program uses a method, class, or field with the @Deprecated annotation. When an element is deprecated, it should also be documented using the Javadoc @deprecated tag, as shown in the following example. The use of the at-sign (@) in both Javadoc comments and in annotations is not coincidental: they are related conceptually. Also, note that the Javadoc tag starts with a lowercased and the annotation starts with an uppercase D.
@Retention annotation specifies how the marked annotation is stored:
RetentionPolicy.SOURCE – The marked annotation is retained only in the source level and is ignored by the compiler.
RetentionPolicy.CLASS – The marked annotation is retained by the compiler at compile time, but is ignored by the Java Virtual Machine (JVM).
RetentionPolicy.RUNTIME – The marked annotation is retained by the JVM so it can be used by the runtime environment.
@Documented annotation indicates that whenever the specified annotation is used those elements should be documented using the Javadoc tool. (By default, annotations are not included in Javadoc.)
But if @Documented is used, it then will be processed by javadoc-like tools and the annotation type information will also be included in the generated document.
Next, we define TestAnnotations class as follows:
There will be annotation-type information for the doSomeTestDocumented() method in the JavaDoc:
This is because of the @Documented tag attached to your Test_Documented annotation.
We just sent you an email. Please click the link in the email to confirm your subscription!