What are Annotations?
It is a new feature of java provided by 5.0 version of java known as Java Annotations. Annotations can be used to describe metadata in java programs. Metadata is the description of the data. When we want to describes some information about coding in java we use annotations. Annotations start with @ followed by the name of the annotation. Now the question arises that how to describe metadata in our java application? As we have comments then why use Annotations?
The answer to above question is that by using comments we can’t access metadata programmatically. To access it programmatically we use annotations in java. It means that at run time we can access metadata. It adds flexibility for the developers to access metadata anywhere programmatically.
Before java 5 annotations we use XML to access metadata during run time, but it has some drawbacks.
Drawbacks of accessing metadata using XML
- First of all being a java developer, you have to learn XML technology.
- You should maintain XML document separately
- Check whether the XML documents are formatted properly or not
- Also, check the XML parsing mechanism to read data from XML
To make metadata accessible during runtime you have to monitor all the things.
Why Java Annotations?
To overcome all these drawbacks we need to go for Java annotations provided by Java 5.0. In replacement of XML documents, we are now able to use java annotations. The web.xml file is mandatory to Execute a Servlet, but with the introduction of the annotations, we are able to remove web.xml from our web applications. web.xml will contains various tags described below.
1 2 3 4 5 6 7 8 9 10 |
<web-app> <servlet> <servlet-name>HelloServlet</servlet-name> <servlet-class>examples.Hello</servlet-class> </servlet> <servlet-mapping> <servlet-name>HelloServlet</servlet-name> <url-pattern>/hello</url-pattern> </servlet-mapping> </web-app> |
After the introduction of the Java annotations now for this entire web.xml we will use only one line of code inside our Java class and that is:
1 2 3 4 5 |
@ WebServlet (“/hello”) public class HelloWorldServlet { . . . . . . } |
J2EE Technologies effectively using Java Annotation
There are various technologies in J2EE which effectively make use of Java annotation which can be seen below
XML VS Annotations
XML | Annotations |
Up to Java 1.4 uses XML | Java 1.5 onwards use Annotations |
Up to JDBC 3.x uses XML | JDBX 4.0 onwards use Annotations |
Up to Servlet 2.5 uses XML | Servlet 3.x onwards use Annotations |
Up to Struts 1.x uses XML | Struts 2.x onwards use Annotations |
Up to JSF 1.x uses XML | JSF 2.x onwards use Annotations |
Up to Hibernate 3.2.4 uses XML | Hibernate 3.2.5 onwards use Annotations |
Up to Spring 2.x uses XML | Spring 3.x onwards use Annotations |
Up to EJB 2.x uses XML | EJB 3.x onwards use Annotations |
Many other uses XML | Many More onwards use Annotations Now |
Java Annotations Syntax:
Annotations are executed by a predefined tool that is APT- Application Processing Tool. Two types of Syntax are available for the annotations:
-
Declaration Syntax:
“@Interface Annotation” is used in this type. It can have members, the syntax for the member is “data_type member _name() [default value]” data_type and member _name are mandatory while default and value are optional.
-
Utilization Syntax:
Programming Elements like classes, variables, methods can be annotated with Java annotations. The syntax for annotation is just above the Programming Element write “@annotation_name (member1=value1, member2=value2, . . . . . )”
Kinds of Java Annotations
On the basis of annotation members available inside the annotations. Annotations are divided into three kinds.
-
Marker Annotation:
Annotations without the members are known as marker annotations. “@Override“, “@WebListener“
-
Single valued Annotation:
Annotations containing only one member is called single valued annotation “@SupressWarnings(“unchecked”)” internally “unchecked” means “value =unchecked“
-
Multi-Valued Annotation:
Annotations declared with more than one member are known as multi-valued annotations. “@WebServlet(name=”helloWorld” urlPattern=”/hello”)“
Classification of Annotation:
General Purpose Annotation
Frequently used in Java applications, this annotation belongs to java.lang package
- @Override
To increases the traceability and readability we use this annotation
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
public class Shape{ public void draw() { System.out.println("draw Shape"); } } public class Square extends Shape{ @Override public void draw() { System.out.println("draw Square"); } } |
- @SuppressWarning
SuppressWarning ignores a specific warning
1 2 3 4 |
@SuppressWarnings("deprecation") void anyFunction() { anyObject.deprecatedMethod(); } |
- @Depricated
Deprecated is used to mark the Programming Element as deprecated
1 2 3 4 |
@Deprecated public void yourMethod(){ // Do something } |
- @FunctionalInterface
FunctionalInterface is used to enforce Single Abstract Method – SAM in an interface.
1 2 3 4 |
@FunctionalInterface public interface SimpleFuncInterface { public void doWork(); } |
Meta-Annotations
“Annotation about Annotations”, Meta-Annotations belongs to java.lang.annotations package
- @ Documented
Documented makes our custom annotations visible in JavaDoc
1 2 3 4 5 6 7 8 9 |
import java.lang.annotation.Documented; @Documented public @interface MyAnnotation { } @MyAnnotation public class MySuperClass { ... } |
- @Inherited
The Inherited annotation tells that a custom annotation used in a class should be inherited by all of its subclasses.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
java.lang.annotation.Inherited @Inherited public @interface MyCustomAnnotation { } @MyCustomAnnotation public class MyParentClass { ... } public class MyChildClass extends MyParentClass { ... } |
- @Target
We have Element Type class which provides us the various Targets. More details can be read from Here , list of them is shown below.
- ElementType.FIELD
- ElementType.METHOD
- ElementType.ANNOTATION_TYPE
- ElementType.PARAMETER
- ElementType.CONSTRUCTOR
- ElementType.TYPE
- ElementType.LOCAL_VARIABLE
- ElementType.PACKAGE
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
import java.lang.annotation.ElementType; import java.lang.annotation.Target; @Target({ElementType.METHOD}) public @interface MyCustomAnnotation { } public class MyClass { @MyCustomAnnotation public void myMethod() { //Doing something } } |
- @ Retention
Retention defines a policy for the Annotation
1 2 3 4 5 6 7 |
<span class="kwd">import</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">lang</span><span class="pun">.</span><span class="pln">annotation</span><span class="pun">.</span><span class="typ">Retention</span><span class="pun">;</span> <span class="kwd">import</span><span class="pln"> java</span><span class="pun">.</span><span class="pln">lang</span><span class="pun">.</span><span class="pln">annotation</span><span class="pun">.</span><span class="typ">RetentionPolicy</span><span class="pun">;</span> <span class="lit">@Retention</span><span class="pun">(</span><span class="typ">RetentionPolicy</span><span class="pun">.</span><span class="pln">RUNTIME</span><span class="pun">)</span> <span class="lit">@interface</span> <span class="typ">MyCustomAnnotation</span> <span class="pun">{</span> <span class="pun">}</span> |
Interface for all the Annotations is java.lang.annotations.Annotation.
Summary:
In this tutorial, we have accomplished following things.
- What is Annotation
- Why Annotation
- Limitations in XML
- Annotations Types and Kinds
- Annotation Examples
Hope that you like this tutorial. Stay tuned for more upcoming tutorials. Stay Blessed!
About Author: Uzair Ahmed Khan is Technical Content Writer, Computer Software Blogger, and Software Engineer. He has a keen eye on Java Technologies other Related Developments.