Defining and Using Annotations Java In Hindi
जावा प्रोग्रामिंग लैंग्वेज में एनोटेशन मेटाडेटा डिटेल क्लास इनफार्मेशन होते हैं, जो यूजर डिफाइन एक कस्टम क्लास, क्लास मेथड, क्लास डाटा टाइप फ़ील्ड, वेरिएबल पैरामीटर आदि के बारे में डिटेल डिजिटल इनफार्मेशन प्रोवाइड करते हैं। इन्हें पहली बार Java 5 वर्जन में पेश किया गया था।

याद रहे, जावा एनोटेशन डायरेक्ट जावा यूजर डिफाइन प्रोग्राम के लॉजिक को मॉडिफाई नहीं करते है, लेकिन जावा के द्वारा एनोटेशन को कंपाइलर, टूल्स, या रनटाइम स्टेप पर रीड किया जा सकता है।
Examples of built-in annotations in the Java programming language.
- @Override Java annotations
- @Deprecated Java annotations
- @SuppressWarnings Java annotations
Using built-in annotations in Java.
@Override जावा एनोटेशन जावा कंपाइलर को यह इंडीकेट करता है कि एक यूजर डिफाइन क्लास मेथड का पर्पस पैरेंट-क्लास मेथड को ओवरराइड करना है।
class Gadgets {
void laptop() {
System.out.println(“Gadgets is laptop”);
}
}
class Hp extends Gadgets {
@Override
void laptop() {
System.out.println(“Hp laptop”);
}
}
यदि यहाँ जावा यूजर मेथड सिग्नेचर में कोई मिस्टेक करते हैं, तो जावा कंपाइलर उन्हें पहचान सकता है।
@Deprecated annotations in Java.
@Deprecated एनोटेशन जावा कंपाइलर को यह इंडीकेट करता है कि किसी यूजर डिफाइन एक कस्टम क्लास या मेथड का अब यूज़ नहीं किया जाना चाहिए।
class Sample {
@Deprecated
void testMethod() {
System.out.println(“This is test method”);
}
}
जावा में @Deprecated एनोटेशन का यूज़ करने से कंपाइलर वॉर्निंग डिस्प्ले हो सकती है।
@SuppressWarnings annotations in Java.
@SuppressWarnings एनोटेशन जावा कंपाइलर को कुछ स्पेसिफिक स्पेशल कंपाइलर वॉर्निंग को हाइड या सप्रेस करने में यूज़ किया जाता है।
@SuppressWarnings(“random”)
void sample() {
// you can write your code
}
Creating a custom user-defined annotation in Java.
जावा यूजर @interface एनोटेशन का यूज़ करके अपना एक सेल्फ कस्टम एनोटेशन क्रिएट कर सकते हैं।
Syntax of a custom user-defined annotation.
@interface AnnotationName {
}
Example of a custom user-defined annotation.
@interface SampleAnnotation {
}
Using it in Java.
@SampleAnnotation
class Employee {
}
Annotations with elements in Java.
एक यूजर डिफाइन जावा एनोटेशन में ऐसे एलिमेंट्स हो सकते हैं, जो जावा प्रोग्राम में डिक्लेअर वेरिएबल पैरामीटर्स के जैसे परफॉर्म या टास्क करते हैं।
@interface EmployeeDetail {
String emp_name();
int emp_age();
}
Using in elements.
@EmployeeDetail(emp_name = “Siddhi Deora”, emp_age = 19)
class Employee {
}
In this example.
emp_name = “Siddhi Deora”
emp_age = 19
यहाँ ये एक कस्टम यूजर डिफाइन एनोटेशन वैल्यू हैं।
Default values in Java annotations.
यूजर डिफाइन जावा एनोटेशन एलिमेंट में डिफ़ॉल्ट वैल्यू हो सकती हैं।
@interface EmployeeDetail {
String emp_name();
int emp_age() default 23;
}
अब जावा यूजर को इसमें ऐज इंडीकेट करने की ज़रूरत नहीं है.
@EmployeeDetail(emp_name = “Bhavishi”)
class Employee {
}
यहाँ एम्प्लॉई की क्लास में ऐज की डिफ़ॉल्ट वैल्यू 23 होगी।
@Retention annotations in Java.
@Retention एनोटेशन जावा कंपाइलर को यह इंडीकेट करता है कि एनोटेशन कितने समय के लिए अवेलेबल है।
There Are Three Retention Java Policies.
| # | @Retention Policy | Detail Meaning of @Retention annotation |
| 1. | SOURCE | The source policy only available in source code mode |
| 2. | CLASS | The class method stored in .class file but not necessarily available at runtime environment |
| 3. | RUNTIME | This is available at runtime through reflection method |
Example of @Retention annotations.
@Retention(RetentionPolicy.RUNTIME)
@interface TestAnnotation {
String value();
}
यह जावा प्रोग्राम में रनटाइम इंस्पेक्शन के लिए, RUNTIME के लिए एसेंशियल है।
@Target annotations in Java.
@Target एनोटेशन जावा कंपाइलर को यह इंडीकेट करता है कि एनोटेशन का यूज़ कहाँ किया जा सकता है।
Example of @Target annotations.
@Target(ElementType.METHOD)
@interface TestAnnotation {
}
अब इसे सिर्फ़ इन क्लास मेथड पर यूज़ किया जा सकता है.
class Test {
@MyTestAnnotation
void preview() {
System.out.println(“Welcome”);
}
}
Common Java class annotations are included in the Element Type value.
- TYPE → Class, interface
- METHOD → Class Method
- FIELD → Class Data Type Field
- PARAMETER → Variable Parameter
- CONSTRUCTOR → User Define Constructor
- LOCAL_VARIABLE → Local Class variable
Reading Java annotations at runtime.
अगर कोई एनोटेशन RUNTIME रिटेंशन का यूज़ करता है, तो रिफ्लेक्शन उसे एक्सेस कर सकता है।
एनोटेशन
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface EmployeeDetail {
String emp_name();
int emp_age();
}
जावा में एनोटेशन का इस्तेमाल करना।
@EmployeeDetail(emp_name = “Harry”, emp_age = 27)
class Employee {
}
जावा में इसे पढ़ना।
Class<Employee> cls = Employee.class;
EmployeeDetail detail =
cls.getAnnotation(EmployeeDetail.class);
System.out.println(detail.emp_name());
System.out.println(detail.emp_age());
Its output.
Harry
27
Example of a complete Java annotation metadata.
import java.lang.annotation.*;
import java.lang.reflect.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
@interface Employee {
String emp_name();
String doj();
}
@Employee (emp_name = “Bhavishi Deora”, doj = “2010-03-01”)
class Person {
}
public class Main {
public static void main(String[] args) {
Class<Person> cls = Person.class;
Employee employee =
cls.getAnnotation(Employee .class);
System.out.println(
“Employee name -: ” + employee.emp_name()
);
System.out.println(
“Date of joining -: ” + employee.doj()
);
}
}
Its output.
Employee name -: Bhavishi Deora
Date of joining -: 2010-03-01
Common java Meta-Annotations information.
जावा प्रोग्रामिंग लैंग्वेज इसके यूजर को ऐसे एनोटेशन प्रोवाइड करता है, जिनका यूज़ दूसरे एनोटेशन को डिफाइन करने में किया जाता है।
| # | Java Meta-Annotation | Purpose of multiple java annotation method |
| 1. | @Retention Annotation | @Retention java annotation used to defines annotation for lifetime method |
| 2. | @Target Annotation | @Target java annotation used to defines, where annotation can be used according to need |
| 3. | @Documented Annotation | @Documented java annotation available o includes annotation in javadoc inbuilt |
| 4. | @Inherited Annotation | @Inherited java annotation allows annotation inheritance by subclasses method |
| 5. | @Repeatable Annotation | @Repeatable java annotation method allows an annotation to be used multiple times when user need |
Flow of Simple Java Annotations
एक कस्टम एनोटेशन को डिफाइन करें
↓
@interface SampleAnnotation
↓
एनोटेशन को अप्लाई करें
↓
@SampleAnnotation
क्लास एम्प्लॉई
↓
एनोटेशन को पढ़ें
↓
रिफ्लेक्शन
↓
एनोटेशन की डिटेल जानकारी
Conclusion of Defining and Using Annotations.
- जावा एनोटेशन यह जावा प्रोग्राम के एलिमेंट के बारे में डिटेल मेटाडेटा इनफार्मेशन होती है।
- कस्टम जावा एनोटेशन @interface मेथड का यूज़ करके क्रिएट किये जाते हैं।
- @Target जावा एनोटेशन कंट्रोल प्रोवाइड करता है कि मौजूदा प्रोग्राम में एनोटेशन कहाँ अप्लाई किया जा सकता है।
- @Retention जावा एनोटेशन कंट्रोल करता है कि इसे कितनी देर तक किसी प्रोग्राम में रखा जाए।
- RUNTIME जावा एनोटेशन मेथड रिटेंशन रिफ्लेक्शन का यूज़ करके एनोटेशन को रीड करने के फीचर्स प्रोवाइड करता है।
- जावा एनोटेशन का यूज़ डिपेंडेंसी इंजेक्शन, टेस्टिंग और ORM सिस्टम जैसे फ्रेमवर्क मे लार्ज लेवल पर किया जाता है।

