Skip to content

Commit 9987819

Browse files
committed
how about this
1 parent 5b88e1c commit 9987819

File tree

1 file changed

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

1 file changed

+16
-7
lines changed

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

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ toc:
1414
- Special methods {functionality}
1515
- Using enums as singletons {singletons}
1616
- Abstract methods in enums {abstract}
17+
- Precautions {precautions}
1718
- Conclusion {conclusion}
1819
description: "Working with enums."
1920
last_update: 2023-09-29
@@ -73,6 +74,9 @@ switch (someDay) {
7374
With [Switch Expressions](id:lang.classes-objects.switch-expression),
7475
the compiler can check whether all values of the enum are handled.
7576
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).
79+
7680
```java
7781
DayOfWeek someDay = DayOfWeek.FRIDAY;
7882

@@ -186,21 +190,26 @@ enum MyEnum {
186190
}
187191
```
188192

189-
<a id="conclusion">&nbsp;</a>
190-
## Conclusion
193+
<a id="changing-instances">&nbsp;</a>
194+
## Precautions
191195

192-
All in all, enums provide a simple and safe way of representing a fixed set of constants
193-
while keeping most of the flexibilities of classes.
194-
195-
However, care should be taken when using enums where the number (or names) of instances is subject to change.
196+
Care should be taken when using enums where the number (or names) of instances is subject to change.
196197
Whenever enum constants are changed, other code expecting the old version of the enum might not work as expected.
197198
This may manifest in compilation errors (e.g. when referencing a removed enum constant),
198199
runtime errors (e.g. if there is a `default` case even though the new enum constant should be handled separately)
199200
or other inconsistencies (e.g. if the value of the enum was saved to a file which is then read and expecting that value to still exist).
201+
200202
When changing enum constants, it is recommended to review all code using the enum.
201203
This is especially important in cases where the enum is also used by other people's code.
202204

203205
Furthermore, it might be worth considering to use other options
204206
in case of many instances since listing a lot of instances at a single location in code can be inflexible.
205207
For example, it may be better to use a configuration file for listing all instances
206-
and reading these configuration files in the program in cases like this.
208+
and reading these configuration files in the program in cases like this.
209+
210+
<a id="conclusion">&nbsp;</a>
211+
## Conclusion
212+
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 another 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).
214+
215+
To learn more, visit the javadoc [`java.lang.Enum`](javadoc:Enum).

0 commit comments

Comments
 (0)