Annotation in Java

Govinda Raj
5 min readDec 20, 2017

‘@’ — annotation.
Aww! What is this? I have heard of class, method, field, variables, interface, comments, java-doc. But what is this? Why it is from Java 1.5, so early? What is use of this keyword? Why do we need this weird thing in programming development? Like these, so many questions must be coming in your mind.

‘@’ provides information about the code and they have no direct effect on the code they annotate.

What???

‘It has no direct effect on the code they annotate’. So why should you know about it if it does not affect the code.

Annotation (‘@’) is metadata about the program embedded in the program itself. Okay I understood that, It’s a metadata, but what type of data it contains?

Program metadata was available through java comments or by Java-doc before annotation. Okay I got it. It is nothing but some descriptive sentences about class or method or interface.

Yes, you hit 4. On top of meta information about source code it can also give instruction to java compiler and it can make metadata to be available at run time so that annotation parser can use it determine the process flow. Now you can hit 6.

So, let’s know about annotation, how differently it works?

  1. We can give instructions to compiler by making annotation.
  2. We can make some annotation which can be available at run time or compile time.

If you open jdk folder and then go to rt.jar folder. In that folder, go to java.lang.annotation package. You will find —

annotation package

Annotation InterfaceThe common interface extended by all annotation types. But an interface that manually extends this one, does not define an annotation type. One more thing, this interface does not itself define an annotation type.

There are 6 ‘@’ files, you can look into left image. So, let’s talk about Target annotation first.

@Target tag is used to specify at which type, the annotation will be used. If you do not define any Target type that means annotation can be applied to any element. It takes ElementType argument whose possible values are —

@Documented — indicates that elements using this annotation should be documented by Java-doc and similar tools.

@Inherited annotation signals that a custom Java annotation used in a class should be inherited by subclasses inheriting from that class.

@Native annotation may be used as a hint by tools that generate native header files to determine whether a header file is required, and if so, what declarations it should contain.
@Repeatable is used to indicate that the annotation type whose declaration it annotates is repeatable.
@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.

Java has three built-in annotations:

  • @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.
  • @Deprecated — indicates that the marked element (class, method or field) is deprecated and should no longer be used. The compiler generates a warning whenever a program uses a method, class, or field that has already been marked with the @Deprecated annotation.
  • @SuppressWarnings — This is just to tell compiler to ignore specific warnings they produce, for example using raw types in generics. It’s retention policy is SOURCE and it gets discarded by compiler.

Now, Let’s look at the @Override annotation’s code —

Something is wrong about @Override code.
It’s not doing anything, how it checks if a method is defined in parent class?

Well, don’t be surprised, I am not kidding you. Override annotation’s definition has that much code only. This is the most important part to understand and I am reiterating myself — Annotations are only metadata and they do not contain any business logic. Tough to digest but true.

If annotations do not contain the logic than someone else must be doing something and that someone is consumer of this annotation metadata. Annotations only provide some information about the attribute (class/method/package/fields) on which it is defined. Consumer is a piece of code which reads this information and then performs necessary logic.

When we are talking about standard annotations like @Override — JVM is the consumer and it works at Byte-Code level. Now that’s something we can’t control and can’t use for custom annotations. So we need to write consumers for our annotations by ourselves. Before that, let’s write our own custom annotation.

Signature for java-annotation —
1. Annotation methods can’t have parameters and it should not have also any throws clauses.
2. Annotation methods return types are limited to primitives, String, Enums, Annotation or array of these.
3. Annotation methods can have default values.
4. We should attach @ just before interface keyword to define annotation. Annotations can have meta annotations attached to them. Meta annotations are used to provide information about the annotation.

There are three types of annotations —
Marker Annotation, Single-Value Annotation, Multi-Value Annotation

Let’s write an example, we will have four elements. We should not provide implementation for these elements.

So far so good. We have defined our custom annotation and applied it to some business logic methods. Now it’s time to write a consumer. For that we will need to use Reflection.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Govinda Raj
Govinda Raj

Written by Govinda Raj

Senior Software Developer. Tech Enthusiast and love coding. My portfolio: https://govinda-raj.github.io/

No responses yet

Write a response