Annotations in Java

Annotations introduced in jdk 1.5 version. Basically, this feature is introduced for the replacement of XML configurations file which we have configured either web applications, enterprise applications or, Applications which uses frameworks such as Struts, Spring, hibernate etc. It was the developer responsibility to provide configuration and other information in the form of XML along with Application. The main problem with XML configuration is verbosity. And by introducing annotations it removes such type of problem.

Annotations represents metadata which is associated with a class or any of its members. This information can either used by the compiler, class loader or by the Runtime Environment.

  • In Java Annotation basically represents starting with “@” symbol.

eg: @Override

It can not change the action of any compiled program.

Annotation basically provide the supplement information of the program.

Annotations can only provide metadata but don’t contain any business logic implementation.

Annotations are divided into two types:

  1. Marker Annotations,
  2. Non-Marker Annotations
  1. Marker Annotations:

Marker Annotations don’t have members and don’t contain data also that means they didn’t carry any information. They simply act as a flag.

Marker annotations are used to divide classes into two categories:

1.1: Classes on which annotations are applied.

1.2: Classes on which annotations are not applied.

Syntax: @AnnotationName

eg: @Override , @Serializable

  • Serializable interface can be replaced by a marker annotation as: @Serializable

Public class Test {

                                                ———

                                                ———-

}

  • Non-marker Annotations:

Syntax: @AnnotationName(memberName=value, …)

Non-marker annotations are used to associate information to its class and its members. Non-marker annotations can contain one or more than one member and also allow the shorthand form of specifying the value of the member. It only requires to specify the value for that member when the annotation will apply and don’t need to specify the name of the member.

eg:

Information required for the management of a servlet can be provided to a web server as:

        @Servlet(name=”servlet1”, url=”loginServlet”)

                Public class LoginServlet {

                                                                ————-

                                                                ————-

                }

  Marker Annotation Non-Marker Annotation
Syntax @interface AnnotationName {} @interface AnnotationName { Type memberName(); ———– ———– }
Example  @interface Serializable @interface Servlet { String name(); String url(); }

Java provides seven annotations in which out of four resides in java.lang.annotation package and 3 of them resides in java.lang package

java.lang.annotation : @Documented, @Retention, @Target, @Inherited

java.lang : @Override, @Deprecated and @SuppressWarning

When we are going to define an annotation, it’s Target Element and Retention Policy need to be specified. Basically Target Element represents the member of the class which can be annotated by the annotation.

@Target – This annotation is used to specify the target of an annotation.

@Retention : This annotation is used to specify Retention policy of an annotation.

Retention Policy specifies whether information of annotation is retained only in java file or carried to any class fileor, to the class object from the class file at the time of loading.

There are basically three retention policies need to apply to define the annotation:

RetentionPolicy.SOURCE,

RetentionPolicy.CLASS,

RetentionPolicy.RUNTIME

Purpose of Retention policy:

  1. RetentionPolicy.SOURCE : Annotation information is retained only in source file.

i.e: It is not stored in class file by the compiler. Such type of annotations are basically ,meant to provide some information to the compiler.

  • RetentionPolicy.CLASS : Annotation information is stored to the class file by the compiler but it’s not loaded into the class object by the class loader. Such annotations are used to provide the information to the class loader.
  • RetentionPolicy.RUNTIME : Annotation information is stored to the class file by the compiler, loaded into class object by the class loader. Such type of annotations are used to runtime environment, web services and frameworks also.

Following methods are used to support annotations under java.lang.Class package:

isAnnotationPresent() This method returns Boolean value . i.e: it’ll return true if specified annotation is applied the class.

Public Boolean isAnnotationPresent(AnnotationClass);

getAnnotation() : This method returns object of specified annotation.

Public Object getAnnotation(AnnotationClass);

Leave a Reply