|
14 | 14 | - Special methods {functionality}
|
15 | 15 | - Using enums as singletons {singletons}
|
16 | 16 | - Abstract methods in enums {abstract}
|
| 17 | +- Precautions {precautions} |
17 | 18 | - Conclusion {conclusion}
|
18 | 19 | description: "Working with enums."
|
19 | 20 | last_update: 2023-09-29
|
@@ -73,6 +74,9 @@ switch (someDay) {
|
73 | 74 | With [Switch Expressions](id:lang.classes-objects.switch-expression),
|
74 | 75 | the compiler can check whether all values of the enum are handled.
|
75 | 76 | 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 | + |
76 | 80 | ```java
|
77 | 81 | DayOfWeek someDay = DayOfWeek.FRIDAY;
|
78 | 82 |
|
@@ -186,21 +190,26 @@ enum MyEnum {
|
186 | 190 | }
|
187 | 191 | ```
|
188 | 192 |
|
189 |
| -<a id="conclusion"> </a> |
190 |
| -## Conclusion |
| 193 | +<a id="changing-instances"> </a> |
| 194 | +## Precautions |
191 | 195 |
|
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. |
196 | 197 | Whenever enum constants are changed, other code expecting the old version of the enum might not work as expected.
|
197 | 198 | This may manifest in compilation errors (e.g. when referencing a removed enum constant),
|
198 | 199 | runtime errors (e.g. if there is a `default` case even though the new enum constant should be handled separately)
|
199 | 200 | 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 | + |
200 | 202 | When changing enum constants, it is recommended to review all code using the enum.
|
201 | 203 | This is especially important in cases where the enum is also used by other people's code.
|
202 | 204 |
|
203 | 205 | Furthermore, it might be worth considering to use other options
|
204 | 206 | in case of many instances since listing a lot of instances at a single location in code can be inflexible.
|
205 | 207 | 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"> </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