Annotations Introduction Tutorial


Annotations Introduction Tutorial

Summary: Annotations are new concept introduced with JDK 1.5. Explained in simple terms for a Beginner with example code in this "Annotations Introduction Tutorial".

Annotations were introduced with JDK 1.5 with the addition of APT (Annotation Processing Tool) tool where you are required to give a separate path for processor tool. Later in JDK 1.6, the tool was integrated with JDK itself; that is, javac can process the annotations. Annotation is a Metadata facility provided to Java Programming language. Annotations initially are very confusing and let us go very slowly to understand them.

Annotations Introduction Tutorial

Annotation is Metadata. What is Metadata?

Many define; in computer terminology, metadata is data about data (though some people do not exactly know the meaning of data about data). The best example of metadata is the comments of JavaDoc. Let us have a good clarity of data about data. Observe.

    final class Demo {    }

What "final" keyword indicates for class Demo. The "final" says the class Demo cannot be inherited by other classes and is not fit for inheritance. "final", we call as access modifier. It gives extra data (information) for other data; here the other data is class Demo. The "final" describes the class Demo. We can say, "final" is metadata. It is the same what an annotation can describe. Annotation gives extra data (additional information) for elements like classes, methods, parameters etc. Annotation processing tool processes the annotations and do accordingly. Annotations add metadata to elements like classes, methods etc. Annotation is separated from the regular code with a prefix of @ symbol.

An annotation is some extra data (called as metadata) added to the source code. What part of source code can be annotated? Annotations can be applied to packages, parameters, variables, methods and classes. Annotation gives special instruction to JDK to process the annotated part differently.

A few words about Annotations

  1. An annotation is the way to place meta data (additional information for compiler or JVM)) to an Java Element like variable, method or class etc.
  2. You can annotate anything (even an expression also). An annotation may have scope and role to do.
  3. Annotation may have parameters.
  4. Annotated information can be retrieved through Java Reflection API methods.
  5. Bugs can be prevented especially when the team size is more and work on the same project.
  6. Annotation is some additional info for compiler or for JVM.
  7. Annotations is a way to add extra properties on a language construct like class, variable, field, method.
  8. Annotations are useful in testing tools also like Junit and @EJB annotation in J2EE.
  9. Annotations are not part of language itself. They are special processing information to a compiler or JRE.
  10. Annotations do not have direct effect on the operation of the code annotated.

Even if I write hundred lines about annotation, it is difficult to understand the concept; the concept is like that. The best way is through an example only.

Program on Java Annotations Introduction Tutorial.

class Test
{
  public void display() {  System.out.println("Hello 1");  }    // observe, display()
}
public class Demo extends Test
{
  public void dasplay() {  System.out.println("Hello 2");  }    // observe, dasplay()

  public static void main(String args[])
  {
    Demo d1 = new Demo();
    d1.display();
  }
}   

Everyone accepts that output is Hello 1.

Here, the programmer wanted to override the display() method of Test in Demo class. But by slip, he mistyped as dasplay() instead of display(). Thinking that he had overridden correctly, calls display() method assuming that he was calling the subclass method with output Hello 2. If he is not aware of the output, he thinks that Hello 1 is the output of the subclass overridden method.

Here what I want is to warn the programmer that he is not overriding the super class display() method. How to give the warning and how to write the code for it? This work can be given to annotation. Annotation can give the warning happily and obverse how much code is to be spent, it is simply just addition of a word, @Override. Annotations are prefixed with @ symbol. Annotation is special kind of modifier. Now, observe the modified code.

1. Using @Override

The above code is modified just with the addition of @Override.

class Test
{
  public void display() {  System.out.println("Hello 1");  }
}
public class Demo extends Test
{
  @Override
  public void dasplay() {  System.out.println("Hello 2");  }

  public static void main(String args[])
  {
    Demo d1 = new Demo();
    d1.display();
  }
}   

Annotations Introduction Tutorial
The difference of the above two programs is just the addition of @Override. @Override is known as annotation. Annotation starts with @ symbol. Now what the annotation @Override has done?

@Override
public void dasplay() {  System.out.println("Hello 2");  }

The compiler checks a method by name dasplay() exists in the super class Test or not. As the method dasplay() is not found, the complier gave the above error message.

Here, @Override describes the data (here, data is a method) of how the compiler should behave if the method is not overridden. For this reason, annotations are called metadata (data about data). Shall I add a small confusion – a) you can annotate an annotation (known as meta-annotation) and b) can create your own annotations (knows as Custom annotations); do not worry right now about these.

Future Java is with annotations only. Already, Hibernate and Spring frameworks are using annotations (I will give a small code later at the end). Annotations make code less, easy and simple to the developer (provided you well understand the purpose of the annotation).

Now who has written the annotation @Override and its meaning to the compiler? It is a predefined annotation built into the language itself from JDK 1.5. Following is the list of built-in annotations. Annotations can be applied to packages, parameters, variables, methods and classes. Annotations can be declared along with the source code and writing like this is known as declarative programming.

Annotations Usage

Annotations have a number of uses of which a few are:

  1. Annotation for the Compiler: Annotations can be for the compiler to detect errors or suppress any warnings.
  2. Annotation for processing at deployment-time: Tools can process annotation information to produce code, XML files etc. Used in frameworks like Spring etc.
  3. Runtime processing: Annotations can be for Java Runtime Environment to examine at runtime.

With this basic knowledge, start to read other "Annotations Introduction Tutorial".

2 thoughts on “Annotations Introduction Tutorial”

  1. Respected Sir,
    I have gone through your annotations stuff. This is very clear and good . But one thing I am not able to understand that we can call final as access modifier. Please conform me that how many access modifier in Java. In our point of view and from Java Doc, there is four access modifiers in Java those are the those are listed below.

    public
    private
    protected
    default

    Please revert me ASAP if possible.

Leave a Comment

Your email address will not be published.