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

Java के Enum का Constructor

Enums एक constructor define कर सकते हैं, जो per constant अपने आप एक बार call होता है, हर constant को construction time पर set की fields के through अपना data रखने देते हुए।
Syntax
java
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

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

Passing Values to Enum Constants

हर enum constant constant के नाम के बाद parentheses में अपने arguments shared constructor को supply कर सकता है, हर constant को अलग associated value रखने देते हुए।

उदाहरण: Passing Values to Enum Constants

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

Adding Fields to an Enum

किसी enum के अंदर declared fields आमतौर पर constructor से set की जाती हैं और final मार्क की जाती हैं, हर constant को अपना permanent data का टुकड़ा देते हुए, जैसे एक numeric code, एक display string, या एक measurement।

उदाहरण: Adding Fields to an Enum

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

Adding Methods to an Enum

एक enum regular methods भी define कर सकता है, बिल्कुल किसी और class की तरह, और वे methods constructor द्वारा set की fields पढ़कर हर constant के लिए specific information compute या return कर सकते हैं।

उदाहरण: Adding Methods to an Enum

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

Enum Constructors are Always Private

एक enum का constructor हमेशा implicitly private है, भले ही कोई access modifier न लिखा गया हो, क्योंकि language खुद गारंटी देती है कि कभी मौजूद हो सकने वाले इकलौते instances enum में declared constants का fixed set हैं।

उदाहरण: Enum Constructors are Always Private

java
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);
	}
}
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. Constructor को public मार्क करना, जो एक compile error है।
  2. new से enum constructor call करना।
  3. Field और एक matching constructor define करना भूल जाना, इसलिए values store नहीं होतीं।

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.