Enums with Fields and Constructors
When you want each enum constant to carry additional information, you can add fields and constructors to your enum. This allows you to associate data with each constant, making your enums far more useful than simple named values. To do this, you declare fields just like in a regular class, and define a constructor that initializes these fields. Each constant then provides values for these fields in its declaration.
Planet.java
123456789101112131415161718192021222324package com.example; public enum Planet { MERCURY(3.303e+23, 2.4397e6), VENUS(4.869e+24, 6.0518e6), EARTH(5.976e+24, 6.37814e6), MARS(6.421e+23, 3.3972e6); private final double mass; // in kilograms private final double radius; // in meters Planet(double mass, double radius) { this.mass = mass; this.radius = radius; } public double getMass() { return mass; } public double getRadius() { return radius; } }
To access the fields of an enum constant, you use the getter methods just as you would for any class. This approach lets you encapsulate related data and logic inside each enum constant, making your code more readable and maintainable. By bundling data with enum constants, you avoid scattered data structures and keep related information together.
Main.java
12345678910111213141516171819202122232425262728293031package com.example; public class Main { public static void main(String[] args) { Planet planet = Planet.EARTH; System.out.println("The mass of " + planet + " is " + planet.getMass() + " kg."); } } enum Planet { MERCURY(3.303e+23, 2.4397e6), VENUS(4.869e+24, 6.0518e6), EARTH(5.976e+24, 6.37814e6), MARS(6.421e+23, 3.3972e6); private final double mass; // in kilograms private final double radius; // in meters Planet(double mass, double radius) { this.mass = mass; this.radius = radius; } public double getMass() { return mass; } public double getRadius() { return radius; } }
1. Can enums in Java have constructors?
2. How do you access a field defined in an enum?
3. What is the purpose of adding fields to an enum?
Kiitos palautteestasi!
Kysy tekoälyä
Kysy tekoälyä
Kysy mitä tahansa tai kokeile jotakin ehdotetuista kysymyksistä aloittaaksesi keskustelumme
Can you show me a code example of an enum with fields and constructors?
How do I use getter methods to access enum fields in Java?
What are some other real-world scenarios where adding fields to enums is useful?
Mahtavaa!
Completion arvosana parantunut arvoon 11.11
Enums with Fields and Constructors
Pyyhkäise näyttääksesi valikon
When you want each enum constant to carry additional information, you can add fields and constructors to your enum. This allows you to associate data with each constant, making your enums far more useful than simple named values. To do this, you declare fields just like in a regular class, and define a constructor that initializes these fields. Each constant then provides values for these fields in its declaration.
Planet.java
123456789101112131415161718192021222324package com.example; public enum Planet { MERCURY(3.303e+23, 2.4397e6), VENUS(4.869e+24, 6.0518e6), EARTH(5.976e+24, 6.37814e6), MARS(6.421e+23, 3.3972e6); private final double mass; // in kilograms private final double radius; // in meters Planet(double mass, double radius) { this.mass = mass; this.radius = radius; } public double getMass() { return mass; } public double getRadius() { return radius; } }
To access the fields of an enum constant, you use the getter methods just as you would for any class. This approach lets you encapsulate related data and logic inside each enum constant, making your code more readable and maintainable. By bundling data with enum constants, you avoid scattered data structures and keep related information together.
Main.java
12345678910111213141516171819202122232425262728293031package com.example; public class Main { public static void main(String[] args) { Planet planet = Planet.EARTH; System.out.println("The mass of " + planet + " is " + planet.getMass() + " kg."); } } enum Planet { MERCURY(3.303e+23, 2.4397e6), VENUS(4.869e+24, 6.0518e6), EARTH(5.976e+24, 6.37814e6), MARS(6.421e+23, 3.3972e6); private final double mass; // in kilograms private final double radius; // in meters Planet(double mass, double radius) { this.mass = mass; this.radius = radius; } public double getMass() { return mass; } public double getRadius() { return radius; } }
1. Can enums in Java have constructors?
2. How do you access a field defined in an enum?
3. What is the purpose of adding fields to an enum?
Kiitos palautteestasi!