RKTechGame | Java Annotations Explained

Java Annotations kya hoti hain?

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.

Annotations ke Types

Custom Annotation Kaise Banaye

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.

Custom Annotation Apply Karna

@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.

Common Predefined Annotations