Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Enums with Fields and Constructors | Advanced Enum Features
Working with Java Enum

bookEnums 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

Planet.java

copy
123456789101112131415161718192021222324
package 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

Main.java

copy
12345678910111213141516171819202122232425262728293031
package 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?

question mark

Can enums in Java have constructors?

Select the correct answer

question mark

How do you access a field defined in an enum?

Select the correct answer

question mark

What is the purpose of adding fields to an enum?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 1

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Suggested prompts:

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?

bookEnums with Fields and Constructors

Sveip for å vise menyen

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

Planet.java

copy
123456789101112131415161718192021222324
package 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

Main.java

copy
12345678910111213141516171819202122232425262728293031
package 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?

question mark

Can enums in Java have constructors?

Select the correct answer

question mark

How do you access a field defined in an enum?

Select the correct answer

question mark

What is the purpose of adding fields to an enum?

Select the correct answer

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 1
some-alt