Java Annotations With Examples

Java Annotations provides information about the code. Java annotations have no direct effect on the code they annotate. In java annotations tutorial, we will look into the following;

  1. Built-in Java annotations
  2. How to write Custom Annotation
  3. Annotations usage and how to parse annotations using Reflection API.

Java Annotations

Java 1.5 introduced annotations and now it’s heavily used in Java EE frameworks like Hibernate, Jersey, and Spring.

Java Annotation is metadata about the program embedded in the program itself. It can be parsed by the annotation parsing tool or by the compiler. We can also specify annotation availability to either compile time only or till runtime.

Before java annotations, program metadata was available through java comments or by Javadoc but annotation offers more than that. Annotations metadata can be available at runtime too and annotation parsers can use it to determine the process flow.

For example, in Jersey webservice we add PATH annotation with URI string to a method and at runtime, jersey parses it to determine the method to invoke for given URI pattern.

Java Custom Annotation

Creating custom annotation is similar to writing an interface, except that the interface keyword is prefixed with @ symbol. We can declare methods in annotation.

Let’s see java custom annotation example and then we will discuss its features and important points.

Some important points about java annotations are:

  1. Annotation methods can’t have parameters.
  2. Annotation methods return types are limited to primitives, String, Enums, Annotation or array of these.
  3. Java Annotation methods can have default values.
  4. Annotations can have meta annotations attached to them. Meta annotations are used to provide information about the annotation.

Meta annotations in java

There are five types of meta annotations:

  1. @Documented – indicates that elements using this annotation should be documented by javadoc and similar tools. This type should be used to annotate the declarations of types whose annotations affect the use of annotated elements by their clients. If a type declaration is annotated with Documented, its annotations become part of the public API of the annotated elements.
  2. @Target – indicates the kinds of program element to which an annotation type is applicable. Some possible values are TYPE, METHOD, CONSTRUCTOR, FIELD etc. If Target meta-annotation is not present, then annotation can be used on any program element.
  3. @Inherited – indicates that an annotation type is automatically inherited. If user queries the annotation type on a class declaration, and the class declaration has no annotation for this type, then the class’s superclass will automatically be queried for the annotation type. This process will be repeated until an annotation for this type is found, or the top of the class hierarchy (Object) is reached.
  4. @Retention – indicates how long annotations with the annotated type are to be retained. It takes RetentionPolicy argument whose Possible values are SOURCE, CLASS and RUNTIME
  5. @Repeatable – used to indicate that the annotation type whose declaration it annotates is repeatable.

Built-in annotations in Java

Java Provides five built-in annotations.

  1. @Override – When we want to override a method of Superclass, we should use this annotation to inform compiler that we are overriding a method. So when superclass method is removed or changed, compiler will show error message. Learn why we should always use java override annotation while overriding a method.
  2. @Deprecated – when we want the compiler to know that a method is deprecated, we should use this annotation. Java recommends that in javadoc, we should provide information for why this method is deprecated and what is the alternative to use.
  3. @SuppressWarnings – This is just to tell compiler to ignore specific warnings they produce, for example using raw types in java generics. It’s retention policy is SOURCE and it gets discarded by compiler.
  4. @FunctionalInterface – This annotation was introduced in Java 8 to indicate that the interface is intended to be a functional interface.
  5. @SafeVarargs – A programmer assertion that the body of the annotated method or constructor does not perform potentially unsafe operations on its varargs parameter.

Java Annotations Example

Let’s see a java example showing the use of built-in annotations in java as well as the use of custom annotation created by us in the above example.

I believe above java annotation example is self-explanatory and showing the use of annotations in different cases.

Java Annotations Parsing

We will use Reflection to parse java annotations from a class. Please note that Annotation Retention Policy should be RUNTIME otherwise its information will not be available at runtime and we won’t be able to fetch any data from it.

Output of the above program is:

Reflection API is very powerful and used widely in Java, J2EE frameworks like Spring, Hibernate, JUnit, check out Reflection in Java.

That’s all for the java annotations example tutorial, I hope you learned something from it.

Java Annotations Updates:

  1. Servlet Specs 3.0 introduced use of annotations for Servlet Configuration and init parameters, read more at Java Servlet Tutorial.
  2. We can use annotations in Struts 2 to configure it’s action classes and result pages, check working example at Struts 2 Hello World Annotation Example.

Reference: Oracle website

By admin

Leave a Reply