DEV Community

Cover image for Get To Know Annotations in Java
Abdulcelil Cercenazi
Abdulcelil Cercenazi

Posted on


Get To Know Annotations in Java

What Are Annotations? 🧐

They allow us to mark (annotate) certain Java language elements as having certain properties (annotations).

What Elements?

  • Package, Class, Method, Field, Parameter.

What Are They Used For? 🤓

There has been a growing trend in the Java world towards annotating elements as having particular attributes that indicate they should be processed in special ways by development tools, deployment tools, or run-time libraries.

Mainly, they are used either on:

Compile time 👇

  • To examine classes and issue compilation errors or warnings.
  • To create new source files.

Run time 👇

  • take actions to do accomplish some task based on the annotations, for example to validate parameters given to a method.

Let's look at two examples using Java annotations 👀

  • Embed instructions for the Java compiler or source code processing tools (please generate getters for my fields using Lombok annotations).
@Getter @Setter  
public class Shipment {  
    private Integer id;  
    private String serialNumber;  
Enter fullscreen mode Exit fullscreen mode
  • Embed instructions that can be read on run time (using reflection) by your app or third party libraries (Spring for example).
public class Application {  
    public static void main(String[] args) { , args);  
Enter fullscreen mode Exit fullscreen mode

Creating Your Own Annotations 🤝

Let's create an annotation called CoolAnnotation which we want to be accessible in compilation time and usable only by types (Classes, Interfaces or Enums).
We also want to add a name value which must be specified by the user.

public @interface CoolAnnotation {  
    String name();  
Enter fullscreen mode Exit fullscreen mode

Let's use it on a class

public class ShipmentService {
    // buisness things 
Enter fullscreen mode Exit fullscreen mode

How To Use This Annotation? 🧠

On Runtime 👈

We can read those annotations at runtime and choose to take actions based on their values

Let's introduce the same cool annotation we had before, but this time let's make it visible on runtime

public @interface CoolAnnotation {  
    String name();  
Enter fullscreen mode Exit fullscreen mode

And annotate it to the service class

public class ShipmentService {  
Enter fullscreen mode Exit fullscreen mode

Now let's test it on runtime

import org.junit.jupiter.api.Test;  
import static org.junit.jupiter.api.Assertions.assertEquals;

public class ShipmentAnnotationTest {  
  public void testAnnotation()  
        String name = ShipmentService  

        assertEquals("myName", name); // lights green 
Enter fullscreen mode Exit fullscreen mode

On Compile time 👈

This is done via compile time annotation processors.

Processors can

  • Create new resource files.

  • Create new code source files.

  • Issue note, warnings and errors which can cause the compilation to fail (picked up by the IDE).

  • They can't modify existing resources or source code files.

Writing a processor ✍️

It's a topic that requires a post on its own, however you can watch this excellent YouTube video on the topic.

Conclusion 👩‍🎓 🧑‍🎓

Annotations allow us to give Java elements some properties (meta-data) that are used either at compile time or runtime.

This offers major benefits to the Java world like offering static type checking at compile time, automated processing at runtime. This is used heavily in frameworks like Spring and Hibernate.

Top comments (0)

Timeless DEV post...

How to write a kickass README

Arguably the single most important piece of documentation for any open source project is the README. A good README not only informs people what the project does and who it is for but also how they use and contribute to it.

If you write a README without sufficient explanation of what your project does or how people can use it then it pretty much defeats the purpose of being open source as other developers are less likely to engage with or contribute towards it.