×
☰ See All Chapters

Java Annotation

Annotations are user defined data type. All annotations extend java.lang.annotations.Annotation interface. Annotations start with ‘@’. Using annotations we can attach additional information (officially called attributes) to class, interface, method or variable. These additional information conveyed by annotation can be used by a development environment, java  compiler or a runtime environment.

We can place Java annotations above classes, interfaces, methods, method parameters, fields and local variables. An annotation which is defined for class should be used over class and cannot be used for methods. But there are some annotations which are defined to use either at class level or method level. There are some annotations which are defined such that they can be used for variable and as well as methods. It all depends on the ways annotations are defined. Usage of annotation to any member should be as per the design. Annotations allow programmer to add metadata to the java class itself instead of providing metadata through XML documents.

We can define 3 types of annotations

1)  Marker annotation: It is similar to marker interface. These annotations contain no members and no data. @Override is an example of Marker Annotation. If we write marker annotation we must write the code at classes, interfaces, methods, method etc... at which annotation is used to process the annotation.

Example: DemoAnnotation();

2)  Single value annotation: When we define an annotation with one member then it is called single value annotation. Specifying the name of the member is optional when we specify the value for that member.

Example: DemoAnnotation(“Hello”);

3)  Multiple value annotation:  When we define an annotation with two or more member then it is called multiple value annotation. Specifying the name of the members is mandatory when we specify the value for that member.

Example: DemoAnnotation(message = “Hello”, sender = “java4coding”, receiver = “Manu”);

Frequently used built-in java Annotations

@Deprecated

This annotation is a marker annotation. It indicates that a declaration is obsolete and has been replaced by a newer form. The @Deprecated annotation is used to mark a class, method or field as deprecated. Adding @Deprecated annotation to any member is a way of giving a message, “should stop using this and replace by a newer alternative form” to the users. If your code uses deprecated classes, methods or fields, the compiler will give you a warning.

java-annotation-0
 

@Override

The @Override Java annotation is used above methods that override methods in a superclass. If the method name and parameters does not match a method in the superclass, the compiler will give you an error.

java-annotation-1
 

@SuppressWarnings

The @SuppressWarnings annotation makes the compiler suppress warnings for a given method.

java-annotation-2
 

@Retention

  • This annotation will be used as annotation to another annotation i.e. it will be used as Meta annotation. 

  • This annotation will be used to specify the retention policy for the annotation which we are defining. There are 3 retention policies: 

1.   SOURCE

2.   CLASS

3.   RUNTIME

  • These three retention policies are available in enum, java.lang.annotation.RetentionPolicy  

Example : @Retention(RetentionPolicy.RUNTIME)

@Retention(RetentionPolicy.SOURCE)

Annotation will be available in source code and it will not be available in bytecode or runtime.

@Retention(RetentionPolicy.CLASS)

Annotation will be available in source code and compilation time but will not be available in runtime.

@Retention(RetentionPolicy.RUNTIME)

Annotation will be available in source code, bytecode and runtime.

@Target

  • This annotation specifies the types of declarations to which an annotation can be applied, means annotation is used to specify the target Placement for the annotation. 

  • Various target points are defined in the enum called Element type which is available in  

java.lang.annotation.ElementType

  • Following are the various constants defined in Element type. 

1.        FIELD

2.        CONSTANT

3.        METHOD

4.        TYPE (class type or interface type)

Custom Annotation

We rarely (99.99% of the time) write custom annotations in real time. All most all we use annotations provided be API providers.  But still let’s see how to write simple custom annotation. We can write custom annotation just like how we write a Demo class.

Example 1:

public @interface Writable {

   boolean value() default false;

}

The above mentioned annotation can be called in the following way.

class DemoClass {

  @Writable(true)

  void meth (int a) {

                System.out.println("Method belongs to DemoClass class");

  }

}

Example 2:

public @interface User {

         String firstName();

         String lastName();

}

The above mentioned “Annotation Type” can be called in the following way.

class DemoClass {

         @User(firstName = "Manu", lastName = "Manjunatha")

         void meth (int a) {

                System.out.println("Method belongs to DemoClass class");

         }

}

 

Example 3:

In this example annotations are used to annotate Annotation Type. These kinds of Annotations (@Retention and @Target) are called as meta-annotations.

@Retention(RetentionPolicy.CLASS)

@Target(ElementType.FIELD)

public @interface TestDemo{

  public String do TestDemo ();

}

 


All Chapters
Author