← Back to Core Java Course | Chapter 13: Advanced Topics & Reference | Lesson 6 of 10

Java के Annotations

Annotations छोटे labels हैं जो आप compiler या दूसरे tools को extra information देने के लिए code पर चिपकाते हैं, fragile कहने वाले एक sticker जैसा। कुछ built in हैं और आप अपना खुद बना सकते हैं।
Syntax
java
@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

java
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();
	}
}

Deprecation Annotations

@Deprecated एक method या class को future उपयोग के लिए discouraged मार्क करता है, call sites पर एक compiler warning trigger करते हुए और दूसरे developers को signal करते हुए कि एक नया alternative मौजूद है।

उदाहरण: Deprecation Annotations

java
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
	}
}

Defining Custom Annotations

एक custom annotation @interface से defined है, optionally elements (जो methods जैसे दिखते हैं) शामिल करते हुए जिन्हें callers annotation apply करते समय named या positional arguments के रूप में भरते हैं।

उदाहरण: Defining Custom Annotations

java
@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");
	}
}

Annotation Retention and Target

@Retention control करता है कि एक annotation runtime तक जीवित रहे या compilation के बाद discarded हो, और @Target restrict करता है कि यह किस तरह की declarations (methods, fields, classes) पर legally apply हो सकता है।

उदाहरण: Annotation Retention and Target

java
// 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();
	}
}

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

java
// 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));
	}
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. यह भूल जाना कि annotations कुछ नहीं करते जब तक कुछ उन्हें process न करे।
  2. RetentionPolicy.SOURCE या CLASS उपयोग करना और runtime पर annotation पढ़ने की उम्मीद करना।
  3. किसी ऐसे method पर @Override लिखना जो कुछ override नहीं करता, जो एक compile error है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.