<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Java_annotation</id>
	<title>Java annotation - Revision history</title>
	<link rel="self" type="application/atom+xml" href="http://debianws.lexgopc.com/wiki143/index.php?action=history&amp;feed=atom&amp;title=Java_annotation"/>
	<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Java_annotation&amp;action=history"/>
	<updated>2026-04-30T22:21:00Z</updated>
	<subtitle>Revision history for this page on the wiki</subtitle>
	<generator>MediaWiki 1.43.1</generator>
	<entry>
		<id>http://debianws.lexgopc.com/wiki143/index.php?title=Java_annotation&amp;diff=2515009&amp;oldid=prev</id>
		<title>imported&gt;ThoughtForge: Corrected formatting</title>
		<link rel="alternate" type="text/html" href="http://debianws.lexgopc.com/wiki143/index.php?title=Java_annotation&amp;diff=2515009&amp;oldid=prev"/>
		<updated>2024-10-28T12:14:25Z</updated>

		<summary type="html">&lt;p&gt;Corrected formatting&lt;/p&gt;
&lt;p&gt;&lt;b&gt;New page&lt;/b&gt;&lt;/p&gt;&lt;div&gt;{{Short description|Syntactic metadata for Java source code}}&lt;br /&gt;
In the [[Java (programming language)|Java computer programming language]], an &amp;#039;&amp;#039;&amp;#039;annotation&amp;#039;&amp;#039;&amp;#039; is a form of syntactic [[metadata]] that can be added to Java [[source code]].&amp;lt;ref&amp;gt;{{cite web|url = http://download.oracle.com/javase/1,5.0/docs/guide/language/annotations.html|title = Annotations|access-date = 2011-09-30|publisher = [[Sun Microsystems]]|archive-url = https://web.archive.org/web/20110925021948/http://download.oracle.com/javase/1,5.0/docs/guide/language/annotations.html|archive-date = 2011-09-25|url-status = dead}}.&amp;lt;/ref&amp;gt; [[Class (computer programming)|Classes]], [[Method (computer programming)|methods]], [[Variable (computer science)|variables]], [[Parameter (computer programming)|parameters]] and [[Java package]]s 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 file]]s generated by the [[Java compiler]].  This allows annotations to be retained by the [[Java virtual machine]] at [[Run time (program lifecycle phase)|run-time]] and read via [[reflection (computer science)|reflection]].&amp;lt;ref&amp;gt;{{Cite book|title = Java(TM) Language Specification|edition = 3rd|publisher = [[Prentice Hall]]|year = 2005|isbn = 0-321-24678-0|url = http://java.sun.com/docs/books/jls/third_edition/html/j3TOC.html|author = Sun Microsystems|author-link = Sun Microsystems}}.&amp;lt;/ref&amp;gt; It is possible to create meta-annotations out of the existing ones in Java.&amp;lt;ref&amp;gt;{{cite web&lt;br /&gt;
 |url=http://www.25hoursaday.com/&lt;br /&gt;
 |title=A COMPARISON OF MICROSOFT&amp;#039;S C# PROGRAMMING LANGUAGE TO SUN MICROSYSTEMS&amp;#039; JAVA PROGRAMMING LANGUAGE: Metadata Annotations&lt;br /&gt;
 |author=Dare Obasanjo&lt;br /&gt;
 |year=2007&lt;br /&gt;
 |publisher=Dare Obasanjo&lt;br /&gt;
 |archive-url=https://web.archive.org/web/20120919093308/http://25hoursaday.com/&lt;br /&gt;
 |archive-date=2012-09-19&lt;br /&gt;
 |access-date=2012-09-20&lt;br /&gt;
 |url-status=dead&lt;br /&gt;
}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== History ==&lt;br /&gt;
The [[Java (software platform)|Java platform]] has various &amp;#039;&amp;#039;ad-hoc&amp;#039;&amp;#039; annotation mechanisms—for example, the &amp;#039;&amp;#039;&amp;lt;code&amp;gt;transient&amp;lt;/code&amp;gt;&amp;#039;&amp;#039; modifier, or the &amp;#039;&amp;#039;&amp;lt;code&amp;gt;@Deprecated&amp;lt;/code&amp;gt;&amp;#039;&amp;#039; javadoc tag. The [[Java Specification Request]] JSR-175 introduced the general-purpose annotation (also known as &amp;#039;&amp;#039;metadata&amp;#039;&amp;#039;) facility to the [[Java Community Process]] in 2002; it gained approval in September 2004.&amp;lt;ref&amp;gt;{{cite web&lt;br /&gt;
|url = http://www.jcp.org/en/jsr/detail?id=175#2&lt;br /&gt;
|title = JSR 175: A Metadata Facility for the JavaTM Programming Language|date = 2006-11-02&lt;br /&gt;
|access-date = 2008-03-05|first = Danny|last = Coward|publisher = [[Java Community Process]]&lt;br /&gt;
}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Annotations became available in the language itself beginning with version 1.5 of the [[Java Development Kit]] (JDK). The [[Annotation processing tool|&amp;lt;code&amp;gt;apt&amp;lt;/code&amp;gt; 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.&lt;br /&gt;
&lt;br /&gt;
== Built-in annotations ==&lt;br /&gt;
Java defines a set of annotations that are built into the language. Of the seven standard annotations, three are part of [[java.lang]], and the remaining four are imported from java.lang.annotation.&amp;lt;ref&amp;gt;{{cite web&lt;br /&gt;
 | url         = https://docs.oracle.com/javase/tutorial/java/annotations/predefined.html&lt;br /&gt;
 | title       = Predefined Annotation Types&lt;br /&gt;
 | publisher   = [[Oracle Corporation]]&lt;br /&gt;
 | access-date = 2016-12-17&lt;br /&gt;
}}&amp;lt;/ref&amp;gt;&amp;lt;ref&amp;gt;{{cite web&lt;br /&gt;
 | url         = http://www.java2s.com/Tutorial/Java/0020__Language/TheBuiltInAnnotations.htm&lt;br /&gt;
 | title       = The Built-In Annotations : Standard Annotations&lt;br /&gt;
 | access-date = 2016-12-17&lt;br /&gt;
}}&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Annotations applied to Java code:&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
* &amp;lt;code&amp;gt;@Override&amp;lt;/code&amp;gt; — 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]].&lt;br /&gt;
* &amp;lt;code&amp;gt;@Deprecated&amp;lt;/code&amp;gt; — Marks the method as obsolete. Causes a compile warning if the method is used.&lt;br /&gt;
* &amp;lt;code&amp;gt;@SuppressWarnings&amp;lt;/code&amp;gt; — Instructs the compiler to suppress the [[compile time]] warnings specified in the annotation parameters.&lt;br /&gt;
&lt;br /&gt;
&amp;#039;&amp;#039;&amp;#039;Annotations applied to other annotations (also known as &amp;quot;Meta Annotations&amp;quot;):&amp;#039;&amp;#039;&amp;#039;&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;@Retention&amp;lt;/code&amp;gt; — Specifies how the marked annotation is stored, whether in code only, compiled into the class, or available at runtime through reflection.&lt;br /&gt;
* &amp;lt;code&amp;gt;@Documented&amp;lt;/code&amp;gt; — Marks another annotation for inclusion in the documentation.&lt;br /&gt;
* &amp;lt;code&amp;gt;@Target&amp;lt;/code&amp;gt; — Marks another annotation to restrict what kind of Java elements the annotation may be applied to.&lt;br /&gt;
* &amp;lt;code&amp;gt;@Inherited&amp;lt;/code&amp;gt; — Marks another annotation to be inherited to subclasses of annotated class (by default annotations are not inherited by subclasses).&lt;br /&gt;
&lt;br /&gt;
Since Java 7, three additional annotations have been added to the language.&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;code&amp;gt;@SafeVarargs&amp;lt;/code&amp;gt; — Suppress warnings for all callers of a method or constructor with a [[Generics in Java|generics]] [[Variadic function|varargs]] parameter, since Java 7.&lt;br /&gt;
* &amp;lt;code&amp;gt;@FunctionalInterface&amp;lt;/code&amp;gt; — Specifies that the [[Declaration (computer programming)|type declaration]] is intended to be a [[Anonymous function|functional interface]], since Java 8.&lt;br /&gt;
* &amp;lt;code&amp;gt;@Repeatable&amp;lt;/code&amp;gt; — Specifies that the annotation can be applied more than once to the same declaration, since Java 8.&lt;br /&gt;
&lt;br /&gt;
== Example ==&lt;br /&gt;
&lt;br /&gt;
=== Built-in annotations ===&lt;br /&gt;
This example demonstrates the use of the &amp;lt;code&amp;gt;@Override&amp;lt;/code&amp;gt; annotation. It instructs the compiler to check parent classes for matching methods. In this case, an error is generated because the &amp;lt;code&amp;gt;gettype()&amp;lt;/code&amp;gt; method of class Cat doesn&amp;#039;t in fact override &amp;lt;code&amp;gt;getType()&amp;lt;/code&amp;gt; of class Animal like is desired, because of the [[Case sensitivity|mismatching case]]. If the &amp;lt;code&amp;gt;@Override&amp;lt;/code&amp;gt; annotation were absent, a new method of name &amp;lt;code&amp;gt;gettype()&amp;lt;/code&amp;gt; would be created in class Cat.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;java&amp;quot;&amp;gt;&lt;br /&gt;
public class Animal {&lt;br /&gt;
    public void speak() {&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public String getType() {&lt;br /&gt;
        return &amp;quot;Generic animal&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
public class Cat extends Animal {&lt;br /&gt;
    @Override&lt;br /&gt;
    public void speak() { // This is a good override.&lt;br /&gt;
        System.out.println(&amp;quot;Meow.&amp;quot;);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    @Override&lt;br /&gt;
    public String gettype() { // Compile-time error due to typo: should be getType() not gettype().&lt;br /&gt;
        return &amp;quot;Cat&amp;quot;;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Custom annotations ===&lt;br /&gt;
&lt;br /&gt;
Annotation type declarations are similar to normal interface declarations. An at-sign (@) precedes the [[Reserved word|keyword]] &amp;quot;interface&amp;quot;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=Java&amp;gt;&lt;br /&gt;
  // @Twizzle is an annotation to method toggle().&lt;br /&gt;
  @Twizzle&lt;br /&gt;
  public void toggle() {&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  // Declares the annotation Twizzle.&lt;br /&gt;
  public @interface Twizzle {&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
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 [[Primitive data type|primitives]], [[String (computer science)|String]], Class, [[Enumerated type|enums]], annotations, and [[Array data type|arrays]] of the preceding types. Methods can have [[Default (computer science)|default values]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=Java&amp;gt;&lt;br /&gt;
  // Same as: @Edible(value = true)&lt;br /&gt;
  @Edible(true)&lt;br /&gt;
  Item item = new Carrot();&lt;br /&gt;
&lt;br /&gt;
  public @interface Edible {&lt;br /&gt;
      boolean value() default false;&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  @Author(first = &amp;quot;Oompah&amp;quot;, last = &amp;quot;Loompah&amp;quot;)&lt;br /&gt;
  Book book = new Book();&lt;br /&gt;
&lt;br /&gt;
  public @interface Author {&lt;br /&gt;
      String first();&lt;br /&gt;
      String last();&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Annotations themselves may be annotated to indicate where and when they can be used:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=Java&amp;gt;&lt;br /&gt;
  @Retention(RetentionPolicy.RUNTIME) // Make this annotation accessible at runtime via reflection.&lt;br /&gt;
  @Target({ElementType.METHOD})       // This annotation can only be applied to class methods.&lt;br /&gt;
  public @interface Tweezable {&lt;br /&gt;
  }&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The compiler reserves a set of special annotations (including &amp;lt;code&amp;gt;@Deprecated&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;@Override&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;@SuppressWarnings&amp;lt;/code&amp;gt;) for syntactic purposes.&lt;br /&gt;
&lt;br /&gt;
Annotations are often used by [[software framework|frameworks]] as a way of conveniently applying behaviours to user-defined classes and methods that must otherwise be declared in an external source (such as an XML configuration file) or programmatically (with API calls). The following, for example, is an annotated [[Java Persistence API|JPA]] data class:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=Java&amp;gt;&lt;br /&gt;
@Entity                                             // Declares this an entity bean&lt;br /&gt;
@Table(name = &amp;quot;people&amp;quot;)                             // Maps the bean to SQL table &amp;quot;people&amp;quot;&lt;br /&gt;
public class Person implements Serializable {&lt;br /&gt;
    @Id                                             // Map this to the primary key column.&lt;br /&gt;
    @GeneratedValue(strategy = GenerationType.AUTO) // Database will generate new primary keys, not us.&lt;br /&gt;
    private Integer id;&lt;br /&gt;
&lt;br /&gt;
    @Column(length = 32)                            // Truncate column values to 32 characters.&lt;br /&gt;
    private String name;&lt;br /&gt;
&lt;br /&gt;
    public Integer getId() {&lt;br /&gt;
        return id;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public void setId(Integer id) {&lt;br /&gt;
        this.id = id;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public String getName() {&lt;br /&gt;
        return name;&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    public void setName(String name) {&lt;br /&gt;
        this.name = name;&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The annotations are not method calls and will not, by themselves, do anything. Rather, the class object is passed to the [[Java Persistence API|JPA]] implementation at [[Run time (program lifecycle phase)|run-time]], which then extracts the annotations to generate an [[object–relational mapping]].&lt;br /&gt;
&lt;br /&gt;
A complete example is given below:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=Java&amp;gt;&lt;br /&gt;
package com.annotation;&lt;br /&gt;
&lt;br /&gt;
import java.lang.annotation.Documented;&lt;br /&gt;
import java.lang.annotation.ElementType;&lt;br /&gt;
import java.lang.annotation.Inherited;&lt;br /&gt;
import java.lang.annotation.Retention;&lt;br /&gt;
import java.lang.annotation.RetentionPolicy;&lt;br /&gt;
import java.lang.annotation.Target;&lt;br /&gt;
&lt;br /&gt;
@Documented&lt;br /&gt;
@Retention(RetentionPolicy.RUNTIME)&lt;br /&gt;
@Target({ElementType.TYPE,ElementType.METHOD,&lt;br /&gt;
         ElementType.CONSTRUCTOR,ElementType.ANNOTATION_TYPE,&lt;br /&gt;
         ElementType.PACKAGE,ElementType.FIELD,ElementType.LOCAL_VARIABLE})&lt;br /&gt;
@Inherited&lt;br /&gt;
&lt;br /&gt;
public @interface Unfinished {&lt;br /&gt;
    public enum Priority { LOW, MEDIUM, HIGH }&lt;br /&gt;
    String value();&lt;br /&gt;
    String[] changedBy() default &amp;quot;&amp;quot;;&lt;br /&gt;
    String[] lastChangedBy() default &amp;quot;&amp;quot;;&lt;br /&gt;
    Priority priority() default Priority.MEDIUM;&lt;br /&gt;
    String createdBy() default &amp;quot;James Gosling&amp;quot;;&lt;br /&gt;
    String lastChanged() default &amp;quot;2011-07-08&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=Java&amp;gt;&lt;br /&gt;
package com.annotation;&lt;br /&gt;
&lt;br /&gt;
public @interface UnderConstruction {&lt;br /&gt;
    String owner() default &amp;quot;Patrick Naughton&amp;quot;;&lt;br /&gt;
    String value() default &amp;quot;Object is Under Construction.&amp;quot;;&lt;br /&gt;
    String createdBy() default &amp;quot;Mike Sheridan&amp;quot;;&lt;br /&gt;
    String lastChanged() default &amp;quot;2011-07-08&amp;quot;;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=Java&amp;gt;&lt;br /&gt;
package com.validators;&lt;br /&gt;
&lt;br /&gt;
import javax.faces.application.FacesMessage;&lt;br /&gt;
import javax.faces.component.UIComponent;&lt;br /&gt;
import javax.faces.context.FacesContext;&lt;br /&gt;
import javax.faces.validator.Validator;&lt;br /&gt;
import javax.faces.validator.ValidatorException;&lt;br /&gt;
&lt;br /&gt;
import com.annotation.UnderConstruction;&lt;br /&gt;
import com.annotation.Unfinished;&lt;br /&gt;
import com.annotation.Unfinished.Priority;&lt;br /&gt;
import com.util.Util;&lt;br /&gt;
&lt;br /&gt;
@UnderConstruction(owner=&amp;quot;Jon Doe&amp;quot;)&lt;br /&gt;
public class DateValidator implements Validator {&lt;br /&gt;
	&lt;br /&gt;
    public void validate(FacesContext context, UIComponent component, Object value)&lt;br /&gt;
        throws ValidatorException {&lt;br /&gt;
        String date = (String) value;&lt;br /&gt;
        String errorLabel = &amp;quot;Please enter a valid date.&amp;quot;;&lt;br /&gt;
        if (!component.getAttributes().isEmpty()) {&lt;br /&gt;
            errorLabel = (String) component.getAttributes().get(&amp;quot;errordisplayval&amp;quot;);&lt;br /&gt;
        }&lt;br /&gt;
&lt;br /&gt;
        if (!Util.validateAGivenDate(date)) {&lt;br /&gt;
            @Unfinished(changedBy = &amp;quot;Steve&amp;quot;,&lt;br /&gt;
                value = &amp;quot;whether to add message to context or not, confirm&amp;quot;,&lt;br /&gt;
                priority = Priority.HIGH&lt;br /&gt;
            )&lt;br /&gt;
            FacesMessage message = new FacesMessage();&lt;br /&gt;
            message.setSeverity(FacesMessage.SEVERITY_ERROR);&lt;br /&gt;
            message.setSummary(errorLabel);&lt;br /&gt;
            message.setDetail(errorLabel);&lt;br /&gt;
            throw new ValidatorException(message);&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== Processing ==&lt;br /&gt;
&lt;br /&gt;
When Java source code is compiled, annotations can be processed by compiler plug-ins called annotation processors. Processors can produce informational messages or create additional Java source files or resources, which in turn may be compiled and processed. However, annotation processors cannot modify the annotated code itself. (Code modifications may be implemented using methods beyond the Java Language Specification.) The Java compiler conditionally stores annotation metadata in the class files, if the annotation has a &amp;lt;code&amp;gt;RetentionPolicy&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;CLASS&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;RUNTIME&amp;lt;/code&amp;gt;. Later, the [[Java virtual machine|JVM]] or other programs can look for the metadata to determine how to interact with the program elements or change their behavior.&lt;br /&gt;
&lt;br /&gt;
In addition to processing an annotation using an annotation processor, a Java programmer can write their own code that uses reflection to process the annotation. [[Java Platform, Standard Edition|Java SE]] 5 supports a new interface that is defined in the &amp;lt;code&amp;gt;java.lang.reflect&amp;lt;/code&amp;gt; package. This package contains the interface called &amp;lt;code&amp;gt;AnnotatedElement&amp;lt;/code&amp;gt; that is implemented by the Java reflection classes including &amp;lt;code&amp;gt;Class&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Constructor&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Field&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;Method&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;Package&amp;lt;/code&amp;gt;. 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.&lt;br /&gt;
&lt;br /&gt;
The &amp;lt;code&amp;gt;AnnotatedElement&amp;lt;/code&amp;gt; interface provides access to annotations having &amp;lt;code&amp;gt;RUNTIME&amp;lt;/code&amp;gt; retention. This access is provided by the &amp;lt;code&amp;gt;getAnnotation&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;getAnnotations&amp;lt;/code&amp;gt;, and &amp;lt;code&amp;gt;isAnnotationPresent&amp;lt;/code&amp;gt; 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:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=Java&amp;gt;&lt;br /&gt;
import java.lang.annotation.Retention;&lt;br /&gt;
import java.lang.annotation.RetentionPolicy;&lt;br /&gt;
&lt;br /&gt;
// This is the annotation to be processed&lt;br /&gt;
// Default for Target is all Java Elements&lt;br /&gt;
// Change retention policy to RUNTIME (default is CLASS)&lt;br /&gt;
@Retention(RetentionPolicy.RUNTIME)&lt;br /&gt;
public @interface TypeHeader {&lt;br /&gt;
    // Default value specified for developer attribute&lt;br /&gt;
    String developer() default &amp;quot;Unknown&amp;quot;;&lt;br /&gt;
    String lastModified();&lt;br /&gt;
    String [] teamMembers();&lt;br /&gt;
    int meaningOfLife();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=Java&amp;gt;&lt;br /&gt;
// This is the annotation being applied to a class&lt;br /&gt;
@TypeHeader(developer = &amp;quot;Bob Bee&amp;quot;,&lt;br /&gt;
    lastModified = &amp;quot;2013-02-12&amp;quot;,&lt;br /&gt;
    teamMembers = { &amp;quot;Ann&amp;quot;, &amp;quot;Dan&amp;quot;, &amp;quot;Fran&amp;quot; },&lt;br /&gt;
    meaningOfLife = 42)&lt;br /&gt;
&lt;br /&gt;
public class SetCustomAnnotation {&lt;br /&gt;
    // Class contents go here&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=Java&amp;gt;&lt;br /&gt;
// This is the example code that processes the annotation&lt;br /&gt;
import java.lang.annotation.Annotation;&lt;br /&gt;
import java.lang.reflect.AnnotatedElement;&lt;br /&gt;
&lt;br /&gt;
public class UseCustomAnnotation {&lt;br /&gt;
    public static void main(String [] args) {&lt;br /&gt;
        Class&amp;lt;SetCustomAnnotation&amp;gt; classObject = SetCustomAnnotation.class;&lt;br /&gt;
        readAnnotation(classObject);&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    static void readAnnotation(AnnotatedElement element) {&lt;br /&gt;
        try {&lt;br /&gt;
            System.out.println(&amp;quot;Annotation element values: \n&amp;quot;);&lt;br /&gt;
            if (element.isAnnotationPresent(TypeHeader.class)) {&lt;br /&gt;
                // getAnnotation returns Annotation type&lt;br /&gt;
                Annotation singleAnnotation = &lt;br /&gt;
                        element.getAnnotation(TypeHeader.class);&lt;br /&gt;
                TypeHeader header = (TypeHeader) singleAnnotation;&lt;br /&gt;
&lt;br /&gt;
                System.out.println(&amp;quot;Developer: &amp;quot; + header.developer());&lt;br /&gt;
                System.out.println(&amp;quot;Last Modified: &amp;quot; + header.lastModified());&lt;br /&gt;
&lt;br /&gt;
                // teamMembers returned as String []&lt;br /&gt;
                System.out.print(&amp;quot;Team members: &amp;quot;);&lt;br /&gt;
                for (String member : header.teamMembers())&lt;br /&gt;
                    System.out.print(member + &amp;quot;, &amp;quot;);&lt;br /&gt;
                System.out.print(&amp;quot;\n&amp;quot;);&lt;br /&gt;
&lt;br /&gt;
                System.out.println(&amp;quot;Meaning of Life: &amp;quot;+ header.meaningOfLife());&lt;br /&gt;
            }&lt;br /&gt;
        } catch (Exception exception) {&lt;br /&gt;
            exception.printStackTrace();&lt;br /&gt;
        }&lt;br /&gt;
    }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== See also ==&lt;br /&gt;
* [[Jakarta Annotations]]&lt;br /&gt;
* [[Metadata (CLI)#Attributes|CLI Attribute]]s&lt;br /&gt;
* [[Java (programming language)|Java]]&lt;br /&gt;
* [[Java virtual machine]]&lt;br /&gt;
* [[Model-driven architecture]]&lt;br /&gt;
* [[Python syntax and semantics#Decorators|Python decorators]], inspired by Java annotations, which have a similar syntax.&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
{{Reflist|2}}&lt;br /&gt;
&lt;br /&gt;
== External links ==&lt;br /&gt;
* [http://java.sun.com/javase/6/docs/technotes/guides/language/annotations.html Introduction to Java 6 Annotations at Sun Developer Network Site]&lt;br /&gt;
* [http://www.developer.com/java/other/article.php/3556176 An Introduction to Java Annotations by M. M. Islam Chisty]&lt;br /&gt;
* {{Cite web |last=Srinivasan |first=Krishna |date=August 11, 2007 |title=Annotations in Java 5.0 |url=http://www.javabeat.net/annotations-in-java-5-0/ |url-status=dead |archive-url=https://web.archive.org/web/20150531080324/http://www.javabeat.net/annotations-in-java-5-0/ |archive-date=May 31, 2015 |website=JavaBeat}}&lt;br /&gt;
* {{Cite web |last=Hunt |first=John |title=Of Java Annotations |url=https://www.theregister.com/2006/02/24/java_annotations/ |date=24 Feb 2006 |website=The Register |language=en}}&lt;br /&gt;
* {{Cite web |date=February 15, 2014 |title=How to create and implement custom annotations in Java? |url=http://www.somanyword.com/2014/02/how-to-create-and-implement-custom-annotations-in-java/ |url-status=dead |archive-url=https://web.archive.org/web/20140223113106/http://www.somanyword.com/2014/02/how-to-create-and-implement-custom-annotations-in-java/ |archive-date=Feb 23, 2014 |website=So Many Word}}&lt;br /&gt;
* {{Cite web |date=October 9, 2014 |title=Java Annotations Tutorial with examples |url=http://www.tutorialsdesk.com/2014/10/java-annotations-tutorial-with-examples.html |website=TutorialsDesk}}&lt;br /&gt;
* {{Cite web |last=Thakor |first=Vicky |date=13 October 2015 |title=Understanding Annotations in Java |url=http://www.javaquery.com/2015/10/understanding-annotations-in-java.html |website=Java by examples}}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--Categories--&amp;gt;&lt;br /&gt;
[[Category:Java (programming language)|annotation]]&lt;br /&gt;
[[Category:Articles with example Java code]]&lt;br /&gt;
[[Category:Java specification requests|annotation]]&lt;/div&gt;</summary>
		<author><name>imported&gt;ThoughtForge</name></author>
	</entry>
</feed>