J2SE 5.0 introduced many new language features in which Annotation is one of them. Annotations are meta-tags that can be added to your programs and can apply to programming elements like declarations, constructors, type declarations,fields, methods, parameters, and variables. As a result, you can indicate whether your methods are dependent on other methods, whether they are incomplete, whether your classes have references to other classes etc. They are considered as additional modifiers without changing the generated code for the corresponding elements. The meta data concept is not new in java ,the comments like @deprecated are already introduced in earlier versions. Annotations allows the compiler or the JVM to extract your program behaviors and generate interdependent codes when necessary.

The annotations introduced in J2SE 5.0 is not with comments, its tagged with the source itself. There are basically three types of annotations in JDK5.

@Override
@Deprecated &
@Suppresswarnings

@Deprecated

@Deprecated in source works the same as @deprecated in the java-doc associated with a class or method. By drooping methods with the @Deprecated tag, you are triggering the compiler to warn the user that these methods are deprecated, when the corresponding method or class is used.

e.g

//method

public class MainClass {
@Deprecated
public static void deprecatedMethod() {
System.out.println(“Don’t use me”);
}
}

//result

Note: file.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

@Override

we use the @Override annotation to flag a method that is supposed to override a method of the super class. This annotation help the programmer from misspelling the method name, specifying the wrong arguments, or using a different return type etc when overriding a particular method.

@SuppressWarnings

It tells the compiler that no need to warn you about certain category of warnings that you know very well or you are expecting . The javac compiler defines seven options to suppress including all, deprecation, unchecked, fallthrough, path, serial, and finally. .

Java Custom Annotations

Java custom annotation follows the general guidelines given below

1.Annotation declaration should start with @ sign, following with interface keyword,then annotation name.

2.Method declaration must not include any parameter.

3.Method declaration must not have any throws .

4.Return types will be any of following primitives

String
Class
enum
Array (any types )

e.g

//declaration

public @interface annot
{
public String theauthor() default “”;
}

//use

public class New_Class
{
@annot(theauthor=”me”)
public void test()
{}
}

Appart from simple and custom annotation java uses another type of annotation know as Meta-Annotations (Annotation Types) which are known as the annotations of annotations, they are of four types.

Target
Retention
Documented
Inherited

The Target annotation

The target annotation tells the compiler about the targeted elements of a class in which the annotation type will be applied.

@Target(ElementType.TYPE)— applied to any element of a class
@Target(ElementType.FIELD)— applied to a fields
@Target(ElementType.METHOD)— applied to a methods
@Target(ElementType.PARAMETER)—applied to the parameters
@Target(ElementType.CONSTRUCTOR)—applied to constructors
@Target(ElementType.LOCAL_VARIABLE)—applied to local variables
@Target(ElementType.ANNOTATION_TYPE)— itself is an annotation type

The retention annotation

The retention annotation tells the compiler where and how long annotations of this type will be retained.There are three types of it

source – the annotation will be discarded post compilation
e.g @Override and @SuppressWarnings
class – the annotation is discarded during class load
runtime – The annotation is never discarded e.g @Deprecated

The inherited annotation

Consider a typical inheritance hierarchy ,where we need to associate a default behavior with interface. We define a custom annotation and add the @Inherited annotation to it ,then add the annotation in the child class . The advantage is that we do not need a intermediate abstract class for any default implementations.

e.g
//declaraion

@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
public @interface Annot
{
public String fun() default “me”;
}

//use

@CarAnnotation

public class BMWCar { }

Annotation is a powerful feature that shift the responsibility for writing the reusable code from the programmer to the compiler and makes it easier to maintain.