Java के Enum का Constructor
In this page:
enum EnumName {
CONSTANT1(value1), CONSTANT2(value2);
private final dataType field;
EnumName(dataType field) { this.field = field; }
}
Enums Can Have Constructors
एक regular class की तरह, एक enum एक constructor define कर सकता है, और Java उस constructor को enum के top पर listed हर constant के लिए एक बार अपने आप call करता है, जब enum type पहली बार load हो उसी moment।
उदाहरण: Enums Can Have Constructors
enum Level {
LOW, MEDIUM, HIGH;
Level() {
System.out.println("Constant created: " + this);
}
}
public class Main {
public static void main(String[] args) {
Level l = Level.LOW; // constructor runs for every constant at class load
}
}
Login to try C/C++/Java/PHP code in the editor
Passing Values to Enum Constants
हर enum constant constant के नाम के बाद parentheses में अपने arguments shared constructor को supply कर सकता है, हर constant को अलग associated value रखने देते हुए।
उदाहरण: Passing Values to Enum Constants
// Define the enum Level (a fixed set of constants)
enum Level {
LOW(1), MEDIUM(2), HIGH(3);
int code;
Level(int code) { this.code = code; }
}
// 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(Level.HIGH.code);
}
}
Login to try C/C++/Java/PHP code in the editor
Adding Fields to an Enum
किसी enum के अंदर declared fields आमतौर पर constructor से set की जाती हैं और final मार्क की जाती हैं, हर constant को अपना permanent data का टुकड़ा देते हुए, जैसे एक numeric code, एक display string, या एक measurement।
उदाहरण: Adding Fields to an Enum
enum Planet {
EARTH(5.97), MARS(0.64);
final double mass; // set by constructor, permanent per constant
Planet(double mass) { this.mass = mass; }
}
public class Main {
public static void main(String[] args) {
System.out.println(Planet.MARS.mass);
}
}
Login to try C/C++/Java/PHP code in the editor
Adding Methods to an Enum
एक enum regular methods भी define कर सकता है, बिल्कुल किसी और class की तरह, और वे methods constructor द्वारा set की fields पढ़कर हर constant के लिए specific information compute या return कर सकते हैं।
उदाहरण: Adding Methods to an Enum
enum Planet {
EARTH(5.97), MARS(0.64);
final double mass;
Planet(double mass) { this.mass = mass; }
double massInKg() {
return mass * 1e21; // uses the field set by the constructor
}
}
public class Main {
public static void main(String[] args) {
System.out.println(Planet.EARTH.massInKg());
}
}
Login to try C/C++/Java/PHP code in the editor
Enum Constructors are Always Private
एक enum का constructor हमेशा implicitly private है, भले ही कोई access modifier न लिखा गया हो, क्योंकि language खुद गारंटी देती है कि कभी मौजूद हो सकने वाले इकलौते instances enum में declared constants का fixed set हैं।
उदाहरण: Enum Constructors are Always Private
enum Level {
LOW, HIGH;
Level() { // implicitly private, even without writing it
}
}
public class Main {
public static void main(String[] args) {
// new Level(); // would not compile: constructor is private
System.out.println(Level.LOW);
}
}
Login to try C/C++/Java/PHP code in the editor
- Constructor को
publicमार्क करना, जो एक compile error है। newसे enum constructor call करना।- Field और एक matching constructor define करना भूल जाना, इसलिए values store नहीं होतीं।
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: