Skip to content

Commit 03123b0

Browse files
committed
update enums article
1 parent ee5507f commit 03123b0

File tree

1 file changed

+9
-8
lines changed
  • app/pages/learn/01_tutorial/03_getting-to-know-the-language/04_classes_objects

1 file changed

+9
-8
lines changed

app/pages/learn/01_tutorial/03_getting-to-know-the-language/04_classes_objects/01_enums.md

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ toc:
1717
- Precautions {precautions}
1818
- Conclusion {conclusion}
1919
description: "Working with enums."
20-
last_update: 2023-09-29
20+
last_update: 2024-06-28
2121
author: ["DanielSchmid"]
2222
---
2323
<a id="intro">&nbsp;</a>
@@ -32,7 +32,7 @@ No instances of the enum can be created outside of enum constants.
3232

3333
```java
3434
public enum DayOfWeek {
35-
// enum constant are listed here:
35+
// enum constants are listed here:
3636
MONDAY, TUESDAY, WEDNESDAY, THURSDAY, FRIDAY, SATURDAY, SUNDAY
3737
}
3838
```
@@ -74,8 +74,8 @@ switch (someDay) {
7474
With [Switch Expressions](id:lang.classes-objects.switch-expression),
7575
the compiler can check whether all values of the enum are handled.
7676
If any possible value is missing in a switch expression, there will be a compiler error.
77-
This is referred to as Exhaustiveness and can also be achieved with regular classes
78-
through [Sealed Classes](https://openjdk.org/jeps/409).
77+
This is referred to as exhaustiveness checking and can also be achieved with regular classes
78+
through [Sealed Classes](https://openjdk.org/jeps/409) and [Patternmatching](/learn/pattern-matching/#switch).
7979

8080
```java
8181
DayOfWeek someDay = DayOfWeek.FRIDAY;
@@ -99,7 +99,8 @@ Arguments to the constructor are passed in parenthesis after the declaration of
9999

100100
```java
101101
public enum DayOfWeek {
102-
MONDAY("MON"), TUESDAY("TUE"), WEDNESDAY("WED"), THURSDAY("THU"), FRIDAY("FRI"), SATURDAY("SAT"), SUNDAY("SUN");
102+
MONDAY("MON"), TUESDAY("TUE"), WEDNESDAY("WED"), THURSDAY("THU"), FRIDAY("FRI"),
103+
SATURDAY("SAT"), SUNDAY("SUN");
103104

104105
private final String abbreviation;
105106

@@ -140,9 +141,9 @@ This allows for comparing instances of enums as well as sorting or searching.
140141
```java
141142
public void compareDayOfWeek(DayOfWeek dayOfWeek){
142143
int comparison = dayOfWeek.compareTo(DayOfWeek.WEDNESDAY);
143-
if ( comparison < 0) {
144+
if (comparison < 0) {
144145
System.out.println("It's before the middle of the work week.");
145-
} else if(comparison > 0){
146+
} else if (comparison > 0) {
146147
System.out.println("It's after the middle of the work week.");
147148
} else {
148149
System.out.println("It's the middle of the work week.");
@@ -210,6 +211,6 @@ and reading these configuration files in the program in cases like this.
210211
<a id="conclusion">&nbsp;</a>
211212
## Conclusion
212213

213-
Enums provide a simple and safe way of representing a fixed set of constants while keeping most of the flexibilities of classes. They are a special type of class that can be used to write code that is elegant, readable, and maintainable, and work well with other newer modern features like [Switch Expressions](id:lang.classes-objects.switch-expression). Another special class is the Record class introduced in Java 19. Visit our [Records tutorial](id:lang.records) to learn more.
214+
Enums provide a simple and safe way of representing a fixed set of constants while keeping most of the flexibilities of classes. They are a special type of class that can be used to write code that is elegant, readable, maintainable and works well with other modern Java features like [Switch Expressions](id:lang.classes-objects.switch-expression). Another special class is the Record class introduced in Java 19. Visit our [Records tutorial](id:lang.records) to learn more.
214215

215216
To learn more about enums, visit the [`java.lang.Enum`](javadoc:Enum) javadoc.

0 commit comments

Comments
 (0)