Java annotation: Difference between revisions

Content deleted Content added
Line 35:
}}</ref>
 
{| class="wikitable"
'''Annotations applied to Java code:'''
! Annotation !! Package !! Description
* <code>@Override</code> — Checks that the method is an [[Method overriding|override]]. Causes a [[compilation error]] if the method is not found in one of the [[parent class]]es or implemented [[Interface (Java)|interfaces]].
|-
* <code>@Deprecated</code> — Marks the method as obsolete. Causes a compile warning if the method is used.
*| <code>@SuppressWarningsDeprecated</code> || Instructs<code>java.lang</code> the|| compilerMarks tothe suppressmethod theas [[compileobsolete. time]]Causes warningsa specifiedcompile inwarning if the annotationmethod is parametersused.
|-
| <code>@FunctionalInterface</code> || <code>java.lang</code> || Marks an interface as intended to be a functional interface.
|-
*| <code>@Override</code> || <code>java.lang</code> || Checks that the method is an [[Method overriding|override]]. Causes a [[compilation error]] if the method is not found in one of the [[parent class]]es or implemented [[Interface (Java)|interfaces]].
|-
*| <code>@SafeVarargs</code> || <code>java.lang</code> || Suppress warnings for all callers of a method or constructor with a [[Generics in Java|generics]] [[Variadic function|varargs]] parameter, since Java 7.
|-
| <code>@SuppressWarnings</code> || <code>java.lang</code> || Instructs the compiler to suppress the [[compile time]] warnings specified in the annotation parameters.
|-
*| <code>@Documented</code> || <code>java.lang.annotation</code> || Marks another annotation for inclusion in the documentation.
|-
| <code>@Inherited</code> || <code>java.lang.annotation</code> || Marks another annotation to be inherited to subclasses of annotated class (by default annotations are not inherited by subclasses).
|-
| <code>@Native</code> || <code>java.lang.annotation</code> || Marks a field defining a constant value as potentially being referenced from native code.
|-
| <code>@Repeatable</code> || <code>java.lang.annotation</code> || Marks another annotation as repeatable.
|-
*| <code>@Retention</code> || <code>java.lang.annotation</code> || Specifies how the marked annotation is stored, whether in code only, compiled into the class, or available at runtime through reflection.
|-
*| <code>@Target</code> || <code>java.lang.annotation</code> || Marks another annotation to restrict what kind of Java elements the annotation may be applied to.
|}
 
In [[Jakarta EE]] (formerly Java Platform, Enterprise Edition), the following annotations also exist in <code>jakarta.annotation</code> (formerly <code>javax.annotation</code>):<ref>{{cite web
'''Annotations applied to other annotations (also known as "Meta Annotations"):'''
| url = https://jakartaee.github.io/common-annotations-api/apidocs/
| publisher = [[Jakarta EE]]
| access-date = 2025-08-13
}}</ref><ref>{{cite web
| url = https://jakarta.ee/specifications/annotations/3.0/annotations-spec-3.0.html
| publisher = [[Jakarta EE]]
| access-date = 2025-08-13
}}</ref>
 
{| class="wikitable"
* <code>@Retention</code> — Specifies how the marked annotation is stored, whether in code only, compiled into the class, or available at runtime through reflection.
! Annotation !! Package !! Description
* <code>@Documented</code> — Marks another annotation for inclusion in the documentation.
|-
* <code>@Target</code> — Marks another annotation to restrict what kind of Java elements the annotation may be applied to.
*| <code>@InheritedGenerated</code> — Marks another|| <code>jakarta.annotation</code> to|| beMarks inheritedsource tocode subclassesthat ofhas annotatedbeen classgenerated (i.e. not written by defaulta annotationsuser, areor notautomatically inheritedgenerated by subclassesa computer).
|-
 
| <code>@Resource</code> || <code>jakarta.annotation</code> || Marks a class, method, or field as a reference to a resource.
Since Java 7, three additional annotations have been added to the language.
|-
 
| <code>@Resources</code> || <code>jakarta.annotation</code> || Declares reference to resources, as a container for multiple resource declarations.
* <code>@SafeVarargs</code> — Suppress warnings for all callers of a method or constructor with a [[Generics in Java|generics]] [[Variadic function|varargs]] parameter, since Java 7.
|-
* <code>@FunctionalInterface</code> — Specifies that the [[Declaration (computer programming)|type declaration]] is intended to be a [[Anonymous function|functional interface]], since Java 8.
| <code>@PostConstruct</code> || <code>jakarta.annotation</code> || Marks a method to indicate that it must be executed after dependency injection to perform initialization, i.e. the method must be invoked before the class is used.
* <code>@Repeatable</code> — Specifies that the annotation can be applied more than once to the same declaration, since Java 8.
|-
| <code>@PreDestroy</code> || <code>jakarta.annotation</code> || Marks a method as a callback notification to indicate the instance is in the process of being removed by the container, i.e. the method is used to release resources held by the instance.
|-
| <code>@Priority</code> || <code>jakarta.annotation</code> || Marks any program element to indicate in what order they should be used.
|-
| <code>@Nonnull</code> || <code>jakarta.annotation</code> || Marks any element that cannot be <code>null</code>.
|-
| <code>@Nullable</code> || <code>java.lang.annotation</code> || Marks any element that has the explicit possibility of being <code>null</code>.
|-
| <code>@RunAs</code> || <code>jakarta.annotation</code> || Defines the security role of the application during execution in a Jakarta EE container.
|-
| <code>@RolesAllowed</code> || <code>jakarta.annotation.security</code> || Marks a method to specify security roles permitted to access the method.
|-
| <code>@PermitAll</code> || <code>jakarta.annotation.security</code> || Marks a method to specify that all security roles may access the method.
|-
| <code>@DenyAll</code> || <code>jakarta.annotation.security</code> || Marks a method to specify that no security roles may access the method.
|-
| <code>@DeclareRoles</code> || <code>jakarta.annotation.security</code> || Specifies security roles used by the application.
|-
| <code>@DataSourceDefinition</code> || <code>jakarta.annotation.sql</code> || Defines a container <code>DataSource</code> that is registered with [[Java Naming and Directory Interface]] (JNDI).
|-
| <code>@DataSourceDefinitions</code> || <code>jakarta.annotation.sql</code> || Declares a container <code>DataSource</code>, acting as a container for multiple data source declarations.
|}
 
== Example ==