FazBrowse Wikipedia Viewer | Main Page |
Search Wikipedia:
|
Viewing: https://en.wikipedia.org/wiki/Java_annotation

Java annotation

In the Java computer programming language, an annotation is a form of syntactic metadata that can be added to Java source code, like an attribute. Classes, methods, variables, parameters and Java packages may be annotated. Like Javadoc tags, Java annotations can be read from source files. Unlike Javadoc tags, Java annotations can also be embedded in and read from Java class files generated by the Java compiler. This allows annotations to be retained by the Java virtual machine at run-time and read via reflection. It is possible to create meta-annotations out of the existing ones in Java.

@FunctionalInterfacejava.langMarks an interface as intended to be a functional interface. @SuppressWarningsjava.langInstructs the compiler to suppress the compile time warnings specified in the annotation parameters. @Nativejava.lang.annotationMarks a field defining a constant value as potentially being referenced from native code. @Targetjava.lang.annotationMarks 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 jakarta.annotation (formerly javax.annotation):

AnnotationPackageDescription
@Resourcesjakarta.annotationDeclares reference to resources, as a container for multiple resource declarations.
@Priorityjakarta.annotationMarks any program element to indicate in what order they should be used.
@RunAsjakarta.annotationDefines the security role of the application during execution in a Jakarta EE container.
@DenyAlljakarta.annotation.securityMarks a method to specify that no security roles may access the method.
@DataSourceDefinitionsjakarta.annotation.sqlDeclares a container DataSource, acting as a container for multiple data source declarations.

There was previously an annotation, // @Twizzle is an annotation to method toggle(). @Twizzle publicvoidtoggle(){ } // Declares the annotation Twizzle. public@interfaceTwizzle{ }

Annotations may include a set of key-value pairs, which are modeled as methods of the annotation type. Each method declaration defines an element of the annotation type. Method declarations must not have any parameters or a throws clause. Return types are restricted to primitives, // Same as: @Edible(value = true) @Edible(true) Itemitem=newCarrot(); public@interfaceEdible{ booleanvalue()defaultfalse; } @Author(first="Oompah",last="Loompah") Bookbook=newBook(); public@interfaceAuthor{ Stringfirst(); Stringlast(); }

Annotations themselves may be annotated to indicate where and when they can be used:

packageorg.wikipedia.examples.annotation; importjava.lang.annotation.Documented; importjava.lang.annotation.ElementType; importjava.lang.annotation.Inherited; importjava.lang.annotation.Retention; importjava.lang.annotation.RetentionPolicy; importjava.lang.annotation.Target; @Documented @Retention(RetentionPolicy.RUNTIME) @Target({ ElementType.TYPE,ElementType.METHOD, ElementType.CONSTRUCTOR,ElementType.ANNOTATION_TYPE, ElementType.PACKAGE,ElementType.FIELD,ElementType.LOCAL_VARIABLE }) @Inherited public@interfaceUnfinished{ publicenumPriority{LOW,MEDIUM,HIGH} Stringvalue(); String[]changedBy()default""; String[]lastChangedBy()default""; Prioritypriority()defaultPriority.MEDIUM; StringcreatedBy()default"James Gosling"; StringlastChanged()default"2011-07-08"; }
packageorg.wikipedia.examples.annotation; importjava.lang.annotation.Retention; importjava.lang.annotation.RetentionPolicy; // This is the annotation to be processed // Default for Target is all Java Elements // Change retention policy to RUNTIME (default is CLASS) @Retention(RetentionPolicy.RUNTIME) public@interfaceTypeHeader{ // Default value specified for developer attribute Stringdeveloper()default"Unknown"; StringlastModified(); String[]teamMembers(); intmeaningOfLife(); }
  • ↑ "Annotations". Sun Microsystems. Archived from the original on 2011-09-25. Retrieved 2011-09-30..
  • ↑ Coward, Danny (2006-11-02). "JSR 175: A Metadata Facility for the JavaTM Programming Language". Java Community Process. Retrieved 2008-03-05.
  • ↑ "Jakarta Annotations API 1.3.5 API". Jakarta EE. Retrieved 2025-08-13.
  • ↑ "Jakarta Annotations". Jakarta EE. Retrieved 2025-08-13.
  • ↑ Srinivasan, Krishna (August 11, 2007). "Annotations in Java 5.0". JavaBeat. Archived from the original on May 31, 2015.
  • "Java Annotations Tutorial with examples". TutorialsDesk. October 9, 2014.

  • View original on Wikipedia | Back | FazBrowse Home