CodeSteps

Python, C, C++, C#, PowerShell, Android, Visual C++, Java ...

Java – Annotations – An Introduction and Standard annotations

Java Annotations (or metadata) are introduced in Java (Java SE5 and above) to give 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, and 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 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 this later.

I am postponing the creation of custom annotations to the next article. Let’s discuss 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 the symbol “@”.? This tells the compiler that, the entry is an annotation. So, the compiler will process the entry, based on the rules applicable to Annotations

Let’s discuss these three built-in annotations.

@Override Annotation

@Override annotation is used to instruct the compiler to ensure to override of 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 the 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 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, use new functionality; we will use this annotation. That means it will inform us to use new functionality rather than the old one.

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

@SuppressWarnings Annotation

@SuppressWarnings annotation is used to suppress unwanted 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 the “all” string value to the @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 types. I am not providing a 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 the serialVersionUID definition on serializable classes.
  • path – To suppress warnings related to non-existent path directories.
  • finally – To suppress warnings about the final 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 the 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 annotations in our upcoming articles.

Thank You.

Java – Annotations – An Introduction and Standard annotations

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll to top