Skip to content

Commit f0fadfe

Browse files
authored
Merge pull request #116 from danthe1st/enum-update
update enums article
2 parents afca448 + 5ee0cce commit f0fadfe

File tree

1 file changed

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

1 file changed

+11
-9
lines changed

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

Lines changed: 11 additions & 9 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-10-02
20+
last_update: 2024-07-08
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
```
@@ -71,11 +71,12 @@ switch (someDay) {
7171
}
7272
```
7373

74-
With [Switch Expressions](id:lang.basics.switch_expressions),
74+
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](jep: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 [pattern matching](/learn/pattern-matching/#switch).
79+
7980

8081
```java
8182
DayOfWeek someDay = DayOfWeek.FRIDAY;
@@ -99,7 +100,8 @@ Arguments to the constructor are passed in parenthesis after the declaration of
99100

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

104106
private final String abbreviation;
105107

@@ -140,9 +142,9 @@ This allows for comparing instances of enums as well as sorting or searching.
140142
```java
141143
public void compareDayOfWeek(DayOfWeek dayOfWeek){
142144
int comparison = dayOfWeek.compareTo(DayOfWeek.WEDNESDAY);
143-
if ( comparison < 0) {
145+
if (comparison < 0) {
144146
System.out.println("It's before the middle of the work week.");
145-
} else if(comparison > 0){
147+
} else if (comparison > 0) {
146148
System.out.println("It's after the middle of the work week.");
147149
} else {
148150
System.out.println("It's the middle of the work week.");
@@ -210,6 +212,6 @@ and reading these configuration files in the program in cases like this.
210212
<a id="conclusion">&nbsp;</a>
211213
## Conclusion
212214

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.basics.switch_expressions). Another special class is the Record class introduced in Java 19. Visit our [Records tutorial](id:lang.records) to learn more.
215+
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.
214216

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

0 commit comments

Comments
 (0)