tl  tr
  Home | Tutorials | Articles | Videos | Products | Tools | Search
Interviews | Open Source | Tag Cloud | Follow Us | Bookmark | Contact   
 Java > Annotations > Basics

Basics 

Annotations are useful to provide meta data about java constructs such as class, interface, enumerations, constructors, fields, methods, method parameters, local variables and packages. Annotations can provide meta data about other Annotations too.

Annotations by themselves wont mean anything. So we need an Annotations processor to perform some tasks using this meta data. The meaning and use of this meta data is application specific.

Java provides three pre-defined annotations.

  • Override
    This annotation is applicable only to methods. It tells the java compiler that the current method is overridden method of parent class or interface. If by any chance the method signature changes in the parent class compiler throws an error.
  • Deprecated
    It tells that the annotated method or any other construct is deprecated and may not be supported in future. Java compiler shows a warning message when such construct is referenced.
  • SuppressWarnings
    Java compiler shows a warning message when a deprecated method is called, a raw Collection is referenced or implemented Serializable interface without explicitly specifying serialVersionUID. To suppress these warnings we can annotate the construct with SuppressWarnings annotation.

Annotation definition is similar to an Interface in that it uses @interface instead of just interface. We can define any number of attributes in Annotation with attribute name as method name. The value would be provided by the client using annotation.

The permitted types for annotation attributes are listed below,

  1. All Primitive types
  2. String
  3. Class
  4. Annotation
  5. Enumeration
  6. 1-Dimensional Arrays

A simple annotation with two String attributes is shown below,

File Name  :  
com/bethecoder/tutorials/annotations/basic/Author.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.annotations.basic;

/**
 * Permitted types for annotation attributes
 
 * 1. primitive type
 * 2. String
 * 3. Class
 * 4. annotation 
 * 5. enumeration 
 * 6. 1-dimensional arrays
 */
public @interface Author {
  String name();
  String creationDate();
}
   

As there is no default value specified for any of the attributes, the client must provide values for both attributes. Its usage is shown below,

File Name  :  
com/bethecoder/tutorials/annotations/basic/Source.java 
Author  :  Sudhakar KV
Email  :  [email protected]
   
package com.bethecoder.tutorials.annotations.basic;

@Author(name="Author1", creationDate="01/01/2500")
public class Source {

  @Author(name="Author2", creationDate="02/01/2500")
  private int fieldOne;
  
  private String fieldTwo;
  
  @Author(name="Author3", creationDate="03/01/2500")
  public Source(int fieldOne, String fieldTwo) {
    super();
    this.fieldOne = fieldOne;
    this.fieldTwo = fieldTwo;
  }

  @Author(name="Author4", creationDate="04/01/2500")
  public int getFieldOne() {
    return fieldOne;
  }
  
  public void setFieldOne(int fieldOne) {
    this.fieldOne = fieldOne;
  }
  
  public String getFieldTwo() {
    return fieldTwo;
  }
  
  public void setFieldTwo(String fieldTwo) {
    this.fieldTwo = fieldTwo;
  }
  
}
   



 
  


  
bl  br