Clean • Professional
Java Annotations are metadata (extra information) added to code such as classes, methods, or variables.
They do not change the actual program logic, but they provide important instructions to the compiler, tools, and runtime environment about how the code should behave.
Annotations are widely used in modern Java applications, especially in frameworks, to make code more clean, structured, and easier to manage.
In simple words: Annotations help developers write cleaner, safer, and more maintainable code by adding extra information without modifying the logic.

Java provides some predefined annotations that are commonly used in development. These annotations help the compiler understand your code better and also reduce common programming mistakes.
@OverrideThe @Override annotation is used when a method overrides a method from its parent class. It tells the compiler that this method is intended to override an existing method.
class Animal {
void sound() {}
}
class Dog extends Animal {
@Override
void sound() {}
}
Benefits:
@Override ensures that the method is actually overriding a method from the parent class@DeprecatedThe @Deprecated annotation is used to mark a method or class as old or not recommended for use. It indicates that this element may be removed in future versions.
@Deprecated
void oldMethod() {}
here,
@Deprecated tells developers that the method or class should no longer be used.@SuppressWarningsThe @SuppressWarnings annotation is used to ignore specific compiler warnings in your code.
@SuppressWarnings("unchecked")
void demo() {}
here,
@SuppressWarnings("unchecked") tells the compiler to ignore unchecked warnings.Java allows you to create your own annotations, which can be used to add custom metadata to your code.
Example:
@interface Info {
String author();
}
Usage:
@Info(author = "Amit")
class Test {}
here,
@interface is used to define a custom annotation.String author(); defines an element (property) inside the annotation.@Info(author = "Amit") is how the annotation is applied to a class.Retention policy defines how long an annotation is available in the program lifecycle.

Example:
import java.lang.annotation.*;
@Retention(RetentionPolicy.SOURCE)
@interface Info {}
@Info
class Test {}
👉 This annotation is not present in the compiled .class file.
.class fileExample:
import java.lang.annotation.*;
@Retention(RetentionPolicy.CLASS)
@interface Info {}
@Info
class Test {}
👉 This annotation is available in the compiled class file, but cannot be accessed using Reflection.
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@interface Info {}
Example with Reflection:
@Info
class Test {}
public class Main {
public static void main(String[] args) {
Info info = Test.class.getAnnotation(Info.class);
System.out.println(info);
}
}
👉 This annotation can be accessed at runtime using Reflection, which makes it the most important type.

Java Annotations play an important role in modern Java development by making code more structured and easier to manage.