Annotations(Java-5)

Simply speaking annotation (Java 5) is nothing but a special comment (like “Please Note!“) kind of label. Purpose of annotation is to provide (Variable / Method / Interface / Class) specific instruction to the Java compiler. Technically speaking, annotation is nothing but an interface with symbol “@” before text “interface”.

Create your own Annotation in Java

package j5.in.janisoftwares;
@Nothing
public class Annotator {
	public static void main(String[] args) {
		System.out.println("Annotator.");
	}
}
package j5.in.janisoftwares;
public @interface Nothing {
}
Annotator.

When there is absolutely nothing (no members / no data) in the annotation, that is called “Marker” annotation. e.g. @Override . It is predefined Java annotation. Others have single / multiple values. Let’s try having some value in it.

package j5.in.janisoftwares;
public @interface Nothing {
	String name() default "NOBODY";
}
package j5.in.janisoftwares;
@Nothing (name="SOMEBODY")
public class Annotator {
	public static void main(String[] args) {
		System.out.println("Annotator.");
	}
}
Annotator.

Annotation by itself is pretty much useless. Annotation with reflection is probably useful.

[John S. Wilfred, 12th Aug 2021]

Notice that regardless of having some attribute added in annotation Class, there is no difference in output whatsoever. So let’s try and use some reflection API in order to make the annotation usage more meaningful by accessing its attributes at RUN_TIME via Reflection API. For that you need total 3 java files.

  1. The Annotation interface itself (in our case let’s say TODO.java)
  2. The one that uses Annotation (Let’s call it Annotator.java)
  3. The one that uses reflection API in order to access annotation info from Annotator.java
package j5.in.janisoftwares;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface TODO {
    String info() default "SOME_DEFAULT_INFO";
    boolean deprecated() default false;
    String developer() default "Anup Jani";
} 
package j5.in.janisoftwares; @TODO(info = "TO BE FIXED LATER", deprecated = true, developer = "John Wilfred") public class Annotator {     public static void main(String[] args){              } }
package j5.in.janisoftwares;
import java.lang.annotation.Annotation;
public class Reflector {
    public static void main(String[] args) {
        Annotator instance = new Annotator();
        Class instanceClass = instance.getClass();
        for (Annotation annotation:instanceClass.getAnnotations()) {
           System.out.println(annotation);
        }
    }
}
Output

Applications of Java Annotations

Reflection + Annotation = Highly re-usable pre-processor code.

Annotations can also help by freeing the coder from writing certain common boiler plate preprocessing logic.

Annotations to the rescue for 3rd Party Frameworks

Spring boot makes extensive use of Annotations in order to inject dependencies. e.g. @Component, @Autowired, etc…

IDEs can enhance the user experience via Annotations.

Integrated Development Environments (IDEs) such as Eclipse can popup-suggest code’s non-compliance to the given annotation even before compiling.

Annotations 4 Annotations

@Retention annotation is used for creating own coder defined custom annotations.

Annotations for Javadoc Help

Java Doc API Help documentation uses annotations for generating comments.