09 December 2023

A Beginner's Guide to Creating Custom Annotations in Java with Indian Flavor

 Annotations in Java are like little notes you can attach to your code to convey extra information. While Java gives us some built-in annotations, we can also create our own custom ones! In this guide, we'll take a dive into the fascinating world of creating our very own custom annotations using simple English and adding a touch of Indian names to make it interesting.

What are Annotations?
Why Make Our Own Annotations?
Let's Make a Custom Annotation: @KaviLekhak

Annotations are like sticky notes you put on your code to tell Java something important. They can be on classes, methods, fields, or even parameters! Think of them as extra information tags.

Custom annotations can be super handy for a few reasons:

  1. Easy Reading: They make your code more readable. It's like giving your code a little more personality!

  2. Code Help: Tools and frameworks can understand your annotations and do cool things with them. It's like giving instructions to your code.


  3. Dynamic Magic: You can make your code do special things at runtime with custom annotations.


  4. Friends with Frameworks: If you've used any Java frameworks, you know they love annotations. Creating your own lets you play well with these frameworks.

In our world of custom annotations, let's create one called @KaviLekhak which means "Poet Writer" in Hindi. This annotation will tell us about the author of a piece of code, the date it was written, any revisions made, and some comments.


import java.lang.annotation.ElementType;

import java.lang.annotation.Retention;

import java.lang.annotation.RetentionPolicy;

import java.lang.annotation.Target;


@Retention(RetentionPolicy.RUNTIME)

@Target(ElementType.METHOD)

public @interface KaviLekhak {

    String lekhak() default "Ananya";

    String tarikh();

    int sudhar() default 1;

    String comments();

}


  • @Retention(RetentionPolicy.RUNTIME): It's like saying, "Hey, remember this annotation even when running the code."


  • @Target(ElementType.METHOD): It tells Java that our annotation is meant for methods.


  • Our @KaviLekhak annotation has four parts: lekhak (author), tarikh (date), sudhar (revision), and comments.

Using the @KaviLekhak Annotation

Let's use our annotation in a sample class:

public class KavyaClass {

@KaviLekhak(lekhak = "Vikram", tarikh = "2023-12-09", sudhar = 2, comments = "Adbhut Kavya")
public void kavyaMethod() {
// Kavya (poetry) implementation
}
}


Here, our kavyaMethod is adorned with the @KaviLekhak annotation, telling us about the poet (author), the date of creation, any revisions made, and some comments.

Checking Annotations at Runtime
To peek at these annotations during runtime, we use a bit of reflection:


import java.lang.reflect.Method; public class AnnotationDarshak { public static void main(String[] args) { KavyaClass kavya = new KavyaClass(); Method[] methods = kavya.getClass().getMethods(); for (Method method : methods) { if (method.isAnnotationPresent(KaviLekhak.class)) { KaviLekhak kaviInfo = method.getAnnotation(KaviLekhak.class); System.out.println("Kavya (Poetry) Method: " + method.getName()); System.out.println("Kavi (Poet): " + kaviInfo.lekhak()); System.out.println("Tarikh (Date): " + kaviInfo.tarikh()); System.out.println("Sudhar (Revision): " + kaviInfo.sudhar()); System.out.println("Comments: " + kaviInfo.comments()); System.out.println(); } } } }

This little code snippet lets us see the annotations in our KavyaClass.

Conclusion

Creating your own annotations in Java is like adding spices to your code. Our @KaviLekhak annotation is just a small example, but you can get creative and make your annotations as per your needs. Remember, annotations are your code's way of expressing itself, so use them wisely and enjoy the simplicity they bring to your Java projects! Happy coding !


No comments:

Post a Comment