Java का Enum
In this page:
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?
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
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
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
Iterating over Enums
आप EnumType.values() से किसी enum में हर constant iterate कर सकते हैं, जो उन्हें declared होने के order में एक array के रूप में return करता है।
यह विशेष रूप से उपयोगी है जब आपको एक dropdown populate करना हो, एक report generate करनी हो, या एक variable के हर possible state में loop करना हो।
उदाहरण: Iterating over Enums
// 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);
}
}
}
Login to try C/C++/Java/PHP code in the editor
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
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
}
}
Login to try C/C++/Java/PHP code in the editor
EnumSet and EnumMap
EnumSet और EnumMap specifically enum keys के लिए बने highly optimized collection implementations हैं, उसी data के लिए एक generic HashSet/HashMap से बेहतर performance प्रदान करते हुए।
उदाहरण: EnumSet and EnumMap
// 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);
}
}
Login to try C/C++/Java/PHP code in the editor
- एक enum value बनाने के लिए
newउपयोग करना, जैसेnew Day(), जो एक compile error है। valueOf()एक गलत नाम के साथ उपयोग करना, जोIllegalArgumentExceptionthrow करता है।newसे एक enum instance बनाने की कोशिश करना, जो एक compile error है।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: