Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Enums with Fields and Constructors | Advanced Enum Features
Practice
Projects
Quizzes & Challenges
Quizzes
Challenges
/
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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

bookEnums with Fields and Constructors

Swipe to show menu

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

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 1
some-alt