If your Java Platform is set to JDK 1.5 or higher, you can use Java annotations in your source code when you are working in the Source Editor. Java annotations can be used in your source code to mark ("annotate") items in your code such as packages, methods and fields. The annotation can be used to provide additional metadata about the annotated code such as resource dependencies, attributes and deployment.
Annotations in the source code are easily recognizable because they are prefixed by the @ symbol. The following is an example of marking a method with the Override annotation type:
@Override public String getDiscountCode() { return this.discountCode; }
When you use the IDE to generate a template or source code, the code generated by the IDE may contain annotations. For example, if you use the IDE to generate an entity class, the IDE generates the following code:
@Entity public class NewEntity implements Serializable { @Id @GeneratedValue(strategy = GenerationType.AUTO) private Long id; }
The @Entity annotation in the code is used to mark the class as an entity class. The @Id and @GeneratedValue annotations are used to mark the id field. In entity classes, annotations are often used instead of deployment descriptors to map fields to database columns.
When you use code completion or templates to generate annotations, the IDE automatically adds the import statements necessary to support the annotations.
You can use the IDE's code completion feature to help you use annotations in your source code. Code completion can suggest annotation usage and generate the annotations in your code. If you type @ and then press Ctrl-Space, the code completion dialog box appears and displays the available annotation types. You can also use code completion to help you specify any necessary annotation members.