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

Java का Enum

एक enum named choices की एक fixed list है, हफ्ते के दिन या traffic light colors जैसा। यह आपको वह उपयोग करने से रोकता है जो list में नहीं है।
Syntax
java
enum EnumName {
    CONSTANT1, CONSTANT2, CONSTANT3
}

EnumName variable = EnumName.CONSTANT1;

What is an Enum?

एक enum constant values का एक fixed, named set define करता है, जैसे DAY या SUIT, आपको compile-time safety देते हुए कि एक variable कभी उन specific values में से सिर्फ एक ही रख सकता है।

चूंकि compiler इस enum type के एक variable को सिर्फ इसके declared constants तक restrict करता है, invalid values सिर्फ discouraged होने के बजाय impossible बन जाती हैं।

उदाहरण: What is an Enum?

java
// Define the enum Day (a fixed set of constants)
enum Day { MONDAY, TUESDAY, WEDNESDAY }
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		Day today = Day.MONDAY;
		// Print a line to the console
		System.out.println(today);
	}
}

Enum with Fields and Methods

Enum constants अपनी खुद की fields और methods रख सकते हैं, हर constant को associated data रखने देते हुए (जैसे एक planet का mass) और उस constant के लिए specific behavior expose करते हुए।

यह एक enum को सिर्फ एक labeled constant से आगे बना देता है, effectively हर value को असली behavior वाला अपना mini-object देते हुए।

उदाहरण: Enum with Fields and Methods

java
// Define the enum Planet (a fixed set of constants)
enum Planet {
	EARTH(5.97), MARS(0.64);
	final double massInYottagrams;
	Planet(double mass) { this.massInYottagrams = mass; }
}
// 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(Planet.EARTH.massInYottagrams);
	}
}

Iterating over Enums

आप EnumType.values() से किसी enum में हर constant iterate कर सकते हैं, जो उन्हें declared होने के order में एक array के रूप में return करता है।

यह विशेष रूप से उपयोगी है जब आपको एक dropdown populate करना हो, एक report generate करनी हो, या एक variable के हर possible state में loop करना हो।

उदाहरण: Iterating over Enums

java
// Define the enum Day (a fixed set of constants)
enum Day { MONDAY, TUESDAY, WEDNESDAY }
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		for (Day d : Day.values()) {
			// Print a line to the console
			System.out.println(d);
		}
	}
}

Enum valueOf and ordinal

valueOf(String) एक matching string को वापस इसके enum constant में convert करता है, अगर कोई match न हो तो IllegalArgumentException throw करते हुए; ordinal() हर constant की zero-based declaration position return करता है।

उदाहरण: Enum valueOf and ordinal

java
enum Day { MONDAY, TUESDAY, WEDNESDAY }
public class Main {
	public static void main(String[] args) {
		Day d = Day.valueOf("TUESDAY");
		System.out.println(d.ordinal()); // 1: zero-based position
	}
}

EnumSet and EnumMap

EnumSet और EnumMap specifically enum keys के लिए बने highly optimized collection implementations हैं, उसी data के लिए एक generic HashSet/HashMap से बेहतर performance प्रदान करते हुए।

उदाहरण: EnumSet and EnumMap

java
// Import java.util.EnumSet so it can be used by its short name
import java.util.EnumSet;
// Define the enum Day (a fixed set of constants)
enum Day { MONDAY, TUESDAY, WEDNESDAY }
// Define the class Main
public class Main {
	// Program entry point: the JVM starts running here
	public static void main(String[] args) {
		EnumSet<Day> weekStart = EnumSet.of(Day.MONDAY, Day.TUESDAY);
		// Print a line to the console
		System.out.println(weekStart);
	}
}
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. एक enum value बनाने के लिए new उपयोग करना, जैसे new Day(), जो एक compile error है।
  2. valueOf() एक गलत नाम के साथ उपयोग करना, जो IllegalArgumentException throw करता है।
  3. new से एक enum instance बनाने की कोशिश करना, जो एक 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.