Annotations ek tarah ke metadata hote hain jo Java code me additional information provide karte hain. Yeh program ke behavior ko directly change nahi karte, lekin compiler ya runtime tools ko information dete hain.
@Override
@SuppressWarnings("unchecked")
@Author(name="John", date="2025-08-26")
@NonNull String name;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation {
String value();
String author() default "RKTechGame";
}
Yeh custom annotation MyAnnotation
hai jisme ek mandatory value
aur ek optional author
element hai.
@MyAnnotation(value = "This class is annotated", author = "User")
public class MyClass {
// Class content yahan
}
Is example me MyClass
class ko humne MyAnnotation
se annotate kiya hai.
@Override
: Method override karne ke liye.@Deprecated
: Purana code indicate karta hai jo future me hata bhi sakte hain.@SuppressWarnings
: Compiler warnings ko suppress karne ke liye.@FunctionalInterface
: Functional interface mark karne ke liye.