transient
modifier, or the @deprecated
javadoc tag. The Java Specification Request JSR-175 introduced the general-purpose annotation (also known as metadata) facility to the Java Community Process in 2002; it gained approval in September 2004.[4]Annotations became available in the language itself beginning with version 1.5 of the Java Development Kit (JDK). The apt
tool provided a provisional interface for compile-time annotation processing in JDK version 1.5; JSR-269 formalized this, and it became integrated into the javac compiler in version 1.6.@Override
- Checks that the method is an override. Causes a compilation error if the method is not found in one of the parent classes or implemented interfaces.@Deprecated
- Marks the method as obsolete. Causes a compile warning if the method is used.@SuppressWarnings
- Instructs the compiler to suppress the compile time warnings specified in the annotation parameters.@Retention
- Specifies how the marked annotation is stored, whether in code only, compiled into the class, or available at runtime through reflection.@Documented
- Marks another annotation for inclusion in the documentation.@Target
- Marks another annotation to restrict what kind of Java elements the annotation may be applied to.@Inherited
- Marks another annotation to be inherited to subclasses of annotated class (by default annotations are not inherited by subclasses).@SafeVarargs
- Suppress warnings for all callers of a method or constructor with a genericsvarargs parameter, since Java 7.@FunctionalInterface
- Specifies that the type declaration is intended to be a functional interface, since Java 8.@Repeatable
- Specifies that the annotation can be applied more than once to the same declaration, since Java 8.@Override
annotation. It instructs the compiler to check parent classes for matching methods. In this case, an error is generated because the gettype()
method of class Cat doesn't in fact override getType()
of class Animal like is desired, because of the mismatching case. If the @Override
annotation was absent, a new method of name gettype()
would be created in class Cat.@Deprecated
, @Override
and @SuppressWarnings
) for syntactic purposes.RetentionPolicy
of CLASS
or RUNTIME
. Later, the JVM or other programs can look for the metadata to determine how to interact with the program elements or change their behavior.java.lang.reflect
package. This package contains the interface called AnnotatedElement
that is implemented by the Java reflection classes including Class
, Constructor
, Field
, Method
, and Package
. The implementations of this interface are used to represent an annotated element of the program currently running in the Java Virtual Machine. This interface allows annotations to be read reflectively.AnnotatedElement
interface provides access to annotations having RUNTIME
retention. This access is provided by the getAnnotation
, getAnnotations
, and isAnnotationPresent
methods. Because annotation types are compiled and stored in byte code files just like classes, the annotations returned by these methods can be queried just like any regular Java object. A complete example of processing an annotation is provided below: