Annotations in Java
×


Annotations in Java

120

🏷️ Annotations in Java

Annotations in Java are a special kind of metadata that you can add to your code—classes, methods, variables, and more. They don't change the program logic directly but provide valuable information for the compiler, development tools, or runtime frameworks. Understanding Annotations in Java is key to writing modern, clean, and maintainable code.

🔎 What Are Annotations?

Annotations act like labels or markers attached to code elements. These markers can influence how code is treated by the compiler or tools. For example, they can signal deprecated code, help with override checks, or instruct the compiler to suppress warnings.

📌 Built-in Annotations You Should Know

Java provides several built-in annotations that are commonly used:

  • @Override: Ensures a method is overriding a superclass method.
  • @Deprecated: Marks code that should no longer be used.
  • @SuppressWarnings: Tells the compiler to ignore specific warnings.

class Parent {
    void show() {
        System.out.println("Parent show");
    }
}

class Child extends Parent {
    @Override
    void show() {
        System.out.println("Child show");
    }
}

🛠️ Creating Custom Annotations

You can define your own annotations to add custom metadata, which can be processed at compile-time or runtime:


import java.lang.annotation.*;

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
public @interface Todo {
    String value();
    String author() default "Unknown";
}

This defines a @Todo annotation applicable to methods, retained at runtime.

🎯 Using Your Custom Annotation


public class MyClass {

    @Todo(value = "Implement this method", author = "Aditya")
    public void unfinishedMethod() {
        // Implementation pending
    }
}

🔄 Annotation Retention Policies

Retention policies specify how long annotations are kept:

  • SOURCE: Discarded by the compiler, used only during compilation.
  • CLASS: Stored in the class file but not available at runtime.
  • RUNTIME: Available during runtime via reflection.

⚙️ Meta-Annotations

Java also offers annotations that apply to other annotations—called meta-annotations. Examples include:

  • @Retention
  • @Target
  • @Inherited
  • @Documented
These help define how your custom annotations behave.

🔍 Accessing Annotations via Reflection

Annotations retained at runtime can be inspected using Java reflection, enabling frameworks to adapt behavior dynamically.


Method method = MyClass.class.getMethod("unfinishedMethod");
if (method.isAnnotationPresent(Todo.class)) {
    Todo todo = method.getAnnotation(Todo.class);
    System.out.println("TODO: " + todo.value() + ", Author: " + todo.author());
}

💡 Why Use Annotations?

  • Simplify configuration compared to XML or manual code.
  • Enable tools and frameworks to automate tasks.
  • Improve code readability and maintainability.
  • Help catch errors early with compiler checks.

🚀 Conclusion

Annotations in Java are a powerful way to add metadata that influences how code is compiled, analyzed, or executed. Whether you use built-in annotations or create custom ones, mastering them allows you to write more robust, maintainable, and modern Java applications.



If you’re passionate about building a successful blogging website, check out this helpful guide at Coding Tag – How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!

For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!



Best WordPress Hosting


Share:


Discount Coupons

Get a .COM for just $6.98

Secure Domain for a Mini Price



Leave a Reply


Comments
    Waiting for your comments

Coding Tag WhatsApp Chat