Java के Annotations
In this page:
@AnnotationName
@AnnotationName(element = value)
returnType methodName() { }
Built-in Standard Annotations
@Override और @FunctionalInterface जैसे Built-in annotations runtime behavior सीधे नहीं बदलते — वे compiler को mistakes पकड़ने देते हैं, जैसे एक mistyped method name जो असल में कुछ override करने में fail होता है।
उदाहरण: Built-in Standard Annotations
class Animal {
void sound() { System.out.println("Some sound"); }
}
class Dog extends Animal {
@Override // compiler catches a mistyped override
void sound() { System.out.println("Bark"); }
}
public class Main {
public static void main(String[] args) {
new Dog().sound();
}
}
Login to try C/C++/Java/PHP code in the editor
Deprecation Annotations
@Deprecated एक method या class को future उपयोग के लिए discouraged मार्क करता है, call sites पर एक compiler warning trigger करते हुए और दूसरे developers को signal करते हुए कि एक नया alternative मौजूद है।
उदाहरण: Deprecation Annotations
class Util {
@Deprecated
static void oldMethod() {
System.out.println("Old behavior");
}
}
public class Main {
public static void main(String[] args) {
Util.oldMethod(); // triggers a compiler warning
}
}
Login to try C/C++/Java/PHP code in the editor
Defining Custom Annotations
एक custom annotation @interface से defined है, optionally elements (जो methods जैसे दिखते हैं) शामिल करते हुए जिन्हें callers annotation apply करते समय named या positional arguments के रूप में भरते हैं।
उदाहरण: Defining Custom Annotations
@interface Author {
String name();
}
@Author(name = "Alice")
// Define the class Report
class Report {
}
// Define the class Main
public class Main {
// Program entry point: the JVM starts running here
public static void main(String[] args) {
// Print a line to the console
System.out.println("Custom annotation applied");
}
}
Login to try C/C++/Java/PHP code in the editor
Annotation Retention and Target
@Retention control करता है कि एक annotation runtime तक जीवित रहे या compilation के बाद discarded हो, और @Target restrict करता है कि यह किस तरह की declarations (methods, fields, classes) पर legally apply हो सकता है।
उदाहरण: Annotation Retention and Target
// Import java.lang.annotation.* so it can be used by its short name
import java.lang.annotation.*;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.METHOD)
@interface Loggable {
}
// Define the class Main
public class Main {
@Loggable
static void run() {
// Print a line to the console
System.out.println("Running");
}
// Program entry point: the JVM starts running here
public static void main(String[] args) {
run();
}
}
Login to try C/C++/Java/PHP code in the editor
Reading Annotations at Runtime
Runtime पर annotations पढ़ने के लिए Java की reflection API चाहिए — एक Class, Method, या Field object पर getAnnotation() call करना यह inspect करने के लिए कि क्या apply किया गया, जो है कि कैसे Spring जैसे frameworks अपनी ज़्यादातर configuration implement करते हैं।
उदाहरण: Reading Annotations at Runtime
// Import java.lang.annotation.* so it can be used by its short name
import java.lang.annotation.*;
// Import java.lang.reflect.Method so it can be used by its short name
import java.lang.reflect.Method;
@Retention(RetentionPolicy.RUNTIME)
@interface Loggable {
}
// Define the class Main
public class Main {
@Loggable
static void run() {}
// Program entry point: the JVM starts running here
public static void main(String[] args) throws NoSuchMethodException {
Method m = Main.class.getDeclaredMethod("run");
// Print a line to the console
System.out.println(m.isAnnotationPresent(Loggable.class));
}
}
Login to try C/C++/Java/PHP code in the editor
- यह भूल जाना कि annotations कुछ नहीं करते जब तक कुछ उन्हें process न करे।
RetentionPolicy.SOURCEयाCLASSउपयोग करना और runtime पर annotation पढ़ने की उम्मीद करना।- किसी ऐसे method पर
@Overrideलिखना जो कुछ override नहीं करता, जो एक compile error है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: