Annotations in Java

Annotations has introduced in jdk 1.5 version. Before that XML was extensively used as metadata, it was quite difficult while having any complex configurations and configurations got difficult while maintenance for XML configuration. For the development point of view, it required something in the place of xml configuration that can be closely coupled with code. To achieve this configuration in the place of xml configuration java introduced Annotations in jdk-1.5. Annotation don’t change the action of a compiled program.

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 {

———

———-

}

  1. Non-marker Annotations:

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.

   Syntax: @AnnotationName(memberName=value, …)

   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 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.

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

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

  1. SOURCE,
  2. CLASS,
  3. RUNTIME

Purpose of Retention policy:

  1. 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.

  1. 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.

 

  1. 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.

Be the first to comment

Leave a Reply