«Back to Home

Core Java

Topics

Annotations In Java

Annotations
 
In Java, annotation is a tag, which shows the metadata that is attached with the class, interface and methods to indicate some additional information, which can be used by Java compiler and JVM.
 
Annotations are mainly used to provide the additional information. Thus, it is an alternative option for XML and Java marker interfaces.

Built-In Java Annotations

In Java, there are many built-in annotations. Some annotations are useful for Java code and few for other annotations.
 
Built-In Java annotations, which are used in Java code are,
  • @Override

  • @SuppressWarnings

  • @Deprecated
Built-In Java Annotations which used in other annotations are,
  • @Target

  • @Retention

  • @Inherited

  • @Documented
Built-In Annotations in Java

@Override
 
@Override annotation checks that the child class method is overriding the parent class method. If it does not, the compile time error occurs. To mark @Override annotation is better because it provides assurance that the method is overridden.
 
Let’s see an example of @Override annotation, given below.
 
Code
  1. class Bike {  
  2.     void run() {  
  3.         System.out.println("Bike run fast");  
  4.     }  
  5. }  
  6.   
  7. class Honda extends Bike {  
  8.     @Override  
  9.     void run() {  
  10.         System.out.println("Honda Bike run fast and smooth");  
  11.     }  
  12. }  
  13. public class AnnotationExample {  
  14.     public static void main(String args[]) {  
  15.         Bike b = new Honda();  
  16.         b.run();  
  17.     }  
  18. }  
26

Output

27

@SuppressWarnings

@SuppressWarnings annotation suppresses the warnings issued by the compiler.
 
Let’s see an example of @SuppressWarnings annotation.
 
Code
  1. import java.util.*;  
  2. public class AnnotationExample {  
  3.     @SuppressWarnings("Unchecked")  
  4.     public static void main(String args[]) {  
  5.         ArrayList a = new ArrayList();  
  6.         a.add("Bob");  
  7.         a.add("Jack");  
  8.         a.add("Mia");  
  9.         for (Object obj: a) {  
  10.             System.out.println(obj);  
  11.         }  
  12.     }  
  13. }  
28
 
Output

29

In the example, mentioned above, there is no warning at the compile time and If we remove the @SuppressWarnings("unchecked") annotation, it will show the warning at the compile time because we are using non-generic collection.
 
@Deprecated

@Deprecated annotation marks that this method is deprecated. Thus, the compiler prints the warning and it informs the user that it may be removed in the future versions. Hence, it is better not to use such methods.
 
Let’s see an example of @Deprecated annotation.
 
Code
  1. class Simple {  
  2.     void a() {  
  3.         System.out.println("a() method is calling...");  
  4.     }  
  5.   
  6.     @Deprecated  
  7.     void b() {  
  8.         System.out.println("b() method is calling...");  
  9.     }  
  10. }  
  11. public class AnnotationExample {  
  12.     public static void main(String args[]) {  
  13.         Simple s = new Simple();  
  14.         s.b();  
  15.     }  
  16. }  
30

Output

31

In the example, mentioned above, program runs normally at the runtime but at the compile time, annotation. Example.java uses or overrides a deprecated API and is recompiled by –Xlint:deprecation for the details.
 
Summary

Thus, we learnt that Java annotations are mainly used to provide the additional information. Thus, it is an alternative option for XML and Java marker interfaces. We also learnt how we can use it in Java.