|
Java™ by example!
|
|
|
What is a typesafe enum in JDK 1.5?
Earlier version of the JDK did not have true enumerated types. Many developers have implemented workarounds for this, a popular one described in Effective Java, item 21. Starting from 1.5, enumerated types have been integrated with the language, allowing developers to write more robust programs without having to write dull boilerplate code, which is automatically generated. A simple example showing some advantages of the new enum feature:
It is possible to iterate over all declared enum constants:
It is even possible to declare methods on enum constants and attach behavior to the constants. DestinationType.java:
Main.java:
Further Information
Author of answer: Joris Van den Bogaert
Comments to this answer are only viewable by members. Login or become a member!
|
|
|
|
|