Beginner’s Guide To Annotations Every Java Developer Should Know

The Java Development Kit (JDK) 5.0 introduces us to the world of annotations in Java and us developers have never been able to get over it. Java Annotations might not have a direct effect, but it certainly makes the life of programmers a lot easier. Let’s check out the most popular annotations every Java developer should know.

List of Java annotations every Java developer should know

While annotations do not have any direct impact on the functioning of the Java codes, they are used to give instruction to the compiler. There are few annotations that are recognized by every compiler and given the highest level of importance. Let’s check out these annotations every Java developer should know.

@Override

This is meant to override the method in an interface (method in an interface cannot be final) or non-final method in a superclass by the one within the subclass. The Override annotation allows the compiler to check whether the implementation of the method was performed correctly by trying to override the method already defined within the superclass or interface. The compiler will throw an error is any one of the following conditions are NOT met –

  1. The method under override annotation successfully overrides a method declared within superclass or interface.
  2. The method under override annotation contains a signature which is override-equivalent to any method declared as public within object.

Here’s an example:

public abstract class Foo {

   public int doSomething() {

       return 1;

   }

   public abstract int doSomethingElse();

}

public class Bar extends Foo {

   @Override

   public int doSomething() {

       return 10;

   }

   @Override

   public int doSomethingElse() {

       return 20;

   }

}

Foo bar = new Bar();

System.out.println(bar.doSomething());         // 10

System.out.println(bar.doSomethingElse());     // 20

 

@SuppressWarnings

While warnings are important, they are not always necessary or need correction. This annotation allows the programmer to suppress such non-threatening warnings. It is important to use it as close to the scope of warning as possible.

@FunctionalInterface

A functional interface is the one that has exactly one abstract method. The Functional Interface annotation ensures that we are not creating non-functional interfaces while meaning to create functional interfaces. Let us check the following functional interfaces:

public interface Foo {

   public int doSomething();

}

public interface Bar {

   public int doSomething();

   public default int doSomethingElse() {

       return 1;

   }

}

The annotation can be added to the above functional interfaces as follows:

@FunctionalInterface

public interface Foo {

   public int doSomething();

}

@FunctionalInterface

public interface Bar {

   public int doSomething();

   public default int doSomethingElse() {

       return 1;

   }

}

If the annotation is added to non-functional interfaces, it will throw error notifications.

@Deprecated

Out-of-date codes should always be removed and not used. These are called deprecated codes. The @Depricated annotation helps in the documentation of deprecated codes and throws a warning if used.

@SafeVarargs

While Varargs are useful and important, it can also cause heap pollution if the generic argument variable type cannot be determined before runtime. However, if the Vararg parameter array is simply transmitting a number of arguments to the method from the caller then it is considered to be safe. Such methods can be annotated under @SafeVarargs to prevent heap pollution warnings.

There is much more to the above annotations and it is important to have in-depth knowledge about these if one is to become a successful Java developer.

Leave a Reply

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