Cheap VPS & Xen Server

Residential Proxy Network - Hourly & Monthly Packages


Java Annotation is a tag that represents the metadata i.e. attached with class, interface, methods or fields to indicate some additional information which can be used by java compiler and JVM.

Annotations in java are used to provide additional information, so it is an alternative option for XML and java marker interfaces.

First, we will learn some built-in annotations then we will move on creating and using custom annotations.

Built-In Java Annotations

There are several built-in annotations in java. Some annotations are applied to java code and some to other annotations.

Built-In Java Annotations used in java code

  • @Override
  • @SuppressWarnings
  • @Deprecated

Built-In Java Annotations used in other annotations

  • @Target
  • @Retention
  • @Inherited
  • @Documented

Understanding Built-In Annotations in java

Let’s understand the built-in annotations first.


@Override annotation assures that the subclass method is overriding the parent class method. If it is not so, compile time error occurs.

Sometimes, we does the silly mistake such as spelling mistakes etc. So, it is better to mark @Override annotation that provides assurity that method is overridden.

  1. class Animal{
  2. void eatSomething(){System.out.println(“eating something”);}
  3. }
  4. class Dog extends Animal{
  5. @Override
  6. void eatsomething(){System.out.println(“eating foods”);}//should be eatSomething
  7. }
  8. class TestAnnotation1{
  9. public static void main(String args[]){
  10. Animal a=new Dog();
  11. a.eatSomething();
  12. }}


Output:Comple Time Error


@SuppressWarnings annotation: is used to suppress warnings issued by the compiler.

  1. import java.util.*;
  2. class TestAnnotation2{
  3. @SuppressWarnings(“unchecked”)
  4. public static void main(String args[]){
  5. ArrayList list=new ArrayList();
  6. list.add(“sonoo”);
  7. list.add(“vimal”);
  8. list.add(“ratan”);
  9. for(Object obj:list)
  10. System.out.println(obj);
  11. }}


Now no warning at compile time.

If you remove the @SuppressWarnings(“unchecked”) annotation, it will show warning at compile time because we are using non-generic collection.


@Deprecated annoation marks that this method is deprecated so compiler prints warning. It informs user that it may be removed in the future versions. So, it is better not to use such methods.

  1. class A{
  2. void m(){System.out.println(“hello m”);}
  3. @Deprecated
  4. void n(){System.out.println(“hello n”);}
  5. }
  6. class TestAnnotation3{
  7. public static void main(String args[]){
  8. A a=new A();
  9. a.n();
  10. }}


At Compile Time:

Note: uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.

At Runtime:

hello n

Custom Annotation

To create and use custom java annotation, visit the next page.