Java: Annotations – An Introduction and Standard annotations

Java Annotations (or metadata) are introduced in Java (Java SE5 and above) to give an additional information to the Java compiler about your Java program. But these are not actually part of the Java code. Annotations are only for representing metadata information. Then what is the use of using Annotations? Java Annotations are used to generate descriptor files or new class definitions, useful to compile time type-checking, various tools will use annotations to generate additional code etc.,.

Can we add annotations to every Java program element.? Yes, we can. We can add Java annotations to classes, class members (constructors, data members, methods), method parameters, local variables, packages, interfaces, enumerated types and even we can annotate annotations.

Annotation is nothing but an instance of an Annotation type; which is a special interface type. An annotation contains a Name, and 0 (zero) or more Members.

If an annotation contains 0 (zero) members, we can call it as a marker annotation. It is a special kind of annotation, used to mark a program element. For example: @Override annotation is a marker annotation. We will discuss about this later.

I am postponing the creation of custom annotations to the next article. Let’s discuss about built-in standard annotations first.

Java provides three standard built-in annotations. These are defined in java.lang package. They are:

  • @Override
  • @Deprecated
  • @SuppressWarnings

Why annotations are started with a symbol “@”.? This tells to the compiler that, the entry is an annotation. So, the compiler will process the entry, based on the rules applicable for Annotations

Let’s discuss about these three built-in annotations.

@Override Annotation

@Override annotation is used to instruct the compiler to ensure to override a method of a base class in its derived class. If the base class method is not overridden in its derived class; when we compile the program the compiler will throw an error.

This way, if we make a common mistake of misspelling a method name or signature mismatch; we will get an error when compiling the program.

It makes our code easier to understand because it is more obvious when methods are overwritten.

And remember that this annotation can be used only on methods.

Is it possible to restrict the annotations to work only on particular program elements, like methods.? Yes it is. We can specify this by using @Target annotation (this annotation is an example of annotate annotations). We are going to discuss about this in our upcoming articles on Annotations.

@Deprecated Annotation

@Deprecated annotation is used to mark the class members as obsolete. If we use deprecated members in our programs, we will get compiler warnings. Usually when we release new versions of our APIs or libraries, it is common to replace new functionality in place of old functionality; to ensure the programs using these new APIs or libraries, to use new functionality; we will use this annotation. That means it will inform to use new functionality rather the old one.

Why we need to maintain both old functionality and new functionality together? Why can’t we remove old functionality and retains only the new one? Because for backward compatibility. Whenever we release new APIs or libraries, the new version should work with even old programs those are developed based on these APIs or libraries. So, we should keep both the functionality together.

@SuppressWarnings Annotation

@SuppressWarnings annotation is used to suppress un-wanted warning messages. For example: if we want to suppress the warnings when we use obsolete methods; we can use this annotation and pass “deprecation” as the value. So, the compiler will not generate any warnings when we use obsolete methods.

How to suppress all the warnings? It is simple; just pass “all” string value to @SuppressWarnings annotation.

@SuppressWarnings(“all”)

We can suppress the following warning types when we compile our programs. Remember that we have to pass these below strings to @SupressWarnings annotation to suppress particular warning type. I am not providing complete list of warning types here; below are some of the common warning types:

  • cast – To suppress casting warnings. casting warnings will come when we write unsafe casting code in our programs.
  • divzero – Suppress division by 0 warning.
  • unchecked – To suppress unchecked warnings. For example: if we use collections without using generics, unchecked warnings will come.
  • serial – To suppress the warnings when we miss serialVersionUID definition on serializable classes.
  • path – To suppress warnings related to non existent path directories.
  • finally – To suppress warnings about the finally clauses that cannot complete normally.
  • fallthrough – We can suppress the warnings generated, when we miss a break statement(s) inside the switch clause.
    @SuppressWarnings(“fallthrough”)
  • deprecation – Suppress warning on using obsolete methods.

Even we can suppress multiple warning types using @SuppressWarnings annotation by passing above strings separated by comma (“,”) and enclosed in flower brackets (“{” and “}”). For example:

@SuppressWarnings({“deprecated”, ”unchecked”, ”fallthrough”})

This will suppress deprecated, unchecked and fallthrough warnings.

We will discuss more about annotations in our upcoming articles.

Thank You.

Leave a Reply