Sei sulla pagina 1di 5

Java annotation - Wikipedia, the free encyclopedia

New features Log in / create account

Article Discussion Read Edit View history Search

Java annotation
From Wikipedia, the free encyclopedia

Main page
This article is written like a manual or guidebook. Please
help rewrite this article from a neutral point of view. (June 2009)
Featured content
Current events This article needs references that appear in reliable
Random article third-party publications. Primary sources or sources affiliated
Donate with the subject are generally not sufficient for a Wikipedia article.
Interaction Please add more appropriate citations from reliable sources. (June
About Wikipedia
Community portal An annotation, in the Java computer programming language, is a special form of syntactic
Recent changes metadata that can be added to Java source code.[1] Classes, methods, variables, parameters and
Contact Wikipedia packages may be annotated. Unlike Javadoc tags, Java annotations can be reflective in that they can
Help be embedded in class files generated by the compiler and may be retained by the Java VM to be
Toolbox made retrievable at run-time.[2]

Print/export Contents [hide]

1 Examples
2 Processing
3 History
4 See also
5 References
6 External links
Português Examples [edit]
// @Twizzle is an annotation to method toggle().
public void toggle() {

// Declares the annotation Twizzle.

public @interface Twizzle {

Annotations may include an optional list of key-value pairs:

// Same as: @Edible(value = true)

Item item = new Carrot();

public @interface Edible {

boolean value() default false;
}[10/14/2010 10:42:28 PM]

Java annotation - Wikipedia, the free encyclopedia

@Author(first = "Oompah", last = "Loompah")

Book book = new Book();

public @interface Author {

String first();
String last();

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

@Retention(RetentionPolicy.RUNTIME) // Make this annotation accessible

at runtime via reflection.
@Target({ElementType.METHOD}) // This annotation can only be
applied to class methods.
public @interface Tweezable {

The compiler reserves a set of special annotations (including @Deprecated, @Override and
@SuppressWarnings) for syntactic purposes.
Annotations are often used by frameworks as a way of conveniently applying behaviours to user-
defined classes and methods that must otherwise be declared in some external source (such as an
XML configuration file) or programmatically (with API calls). The following, for example, is an
annotated EJB 3.0 data class:

@Entity // Declares this an

entity bean
@Table(name = "people") // Maps the bean to
SQL table "people"
class Person implements Serializable {
@Id // Map this to the
primary key column.
@GeneratedValue(strategy = GenerationType.AUTO) // Database will
generate new primary keys, not us.
private Integer id;

@Column(length = 32) // Truncate column

values to 32 characters.
private String name;

public Integer getId() {

return id;

public void setId(Integer id) { = id;

public String getName() {

return name;

public void setName(String name) { = name;

A complete example is given below:

package com.annotation;

import java.lang.annotation.Documented;[10/14/2010 10:42:28 PM]

Java annotation - Wikipedia, the free encyclopedia

import java.lang.annotation.ElementType;
import java.lang.annotation.Inherited;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;


public @interface Unfinished {

public enum Priority { LOW, MEDIUM, HIGH }
String value();
String[] changedBy() default "";
String[] lastChangedBy() default "";
Priority priority() default Priority.MEDIUM;
String createdBy() default "prabir karmakar";
String lastChanged() default "01/01/2009";

package com.annotation;

public @interface UnderConstruction {

String owner() default "PrabirK";
String value() default "Object is Under Construction.";
String createdBy() default "Prabir Karmakar";
String lastChanged() default "01/01/2009";

package com.validators;

import javax.faces.application.FacesMessage;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.validator.Validator;
import javax.faces.validator.ValidatorException;

import com.annotation.UnderConstruction;
import com.annotation.Unfinished;
import com.annotation.Unfinished.Priority;
import com.util.Util;

@UnderConstruction(owner="Prabir Karmakar")
public class DateValidator implements Validator{

public void validate(FacesContext context, UIComponent

component, Object value)
throws ValidatorException
String date = (String) value;
String errorLabel = "Please enter a valid date.";
errorLabel = (String)

{[10/14/2010 10:42:28 PM]

Java annotation - Wikipedia, the free encyclopedia

@Unfinished(changedBy = "prabirk"
,value="whether to add message to
context or not, confirm"
FacesMessage message = new FacesMessage();

throw new ValidatorException(message);


The annotations are not method calls and will not, by themselves, do anything. Rather, the class
object is passed to the EJB implementation at run-time, which then extracts the annotations to
generate an ORM.

Processing [edit]

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, but annotation processors
cannot modify the annotated code itself. The Java compiler conditionally stores annotation metadata
in the class files if the annotation has a 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.

History [edit]

The Java platform has had various ad-hoc annotation mechanisms—for example, the transient
modifier, or the @deprecated javadoc tag. The general purpose annotation (also known as metadata)
facility was introduced to the Java Community Process as JSR-175 in 2002 and approved in
September 2004. [3] Annotations became available in the language itself beginning with version 1.5 of
the JDK. A provisional interface for compile-time annotation processing was provided by the apt tool
in JDK version 1.5, and was formalized through JSR-269 and integrated into the javac compiler in
version 1.6.

See also [edit]

Java programming
Model-driven architecture
Java virtual machine
.NET Attributes

References [edit]

1. ^ "JDK 5.0 Developer's Guide: Annotations" . Hall. ISBN 0321246780.

Sun Microsystems. 2007-12-18. Retrieved 3. ^ Coward, Danny (2006-11-02). "JSR 175: A
2008-03-05.. Metadata Facility for the JavaTM Programming
2. ^ Sun Microsystems (2005). Java(TM) Language" . Java Community Process.
Language Specification (3rd ed.). Prentice Retrieved 2008-03-05.

External links [edit][10/14/2010 10:42:28 PM]

Java annotation - Wikipedia, the free encyclopedia

Introduction to Java 5.0 Annotations by Joy Christy

Of Java Annotations by John Hunt
An Introduction to Java Annotations by M. M. Islam Chisty
Introduction to Java 6 Annotations at Sun Developer Network Site

Categories: Java programming language

This page was last modified on 26 August 2010 at 23:59.

Text is available under the Creative Commons Attribution-ShareAlike License; additional terms may apply. See Terms of
Use for details.
Wikipedia® is a registered trademark of the Wikimedia Foundation, Inc., a non-profit organization.

Contact us

Privacy policy About Wikipedia Disclaimers[10/14/2010 10:42:28 PM]