How an Enum will look like ,
Arbitrary fields may be added to enum classes, and to individual enum constants. The ability to add such fields will only be used by relatively sophisticated programmers, but greatly enhances the power of the facility. It adds nothing to the complexity of the facility for programmers who don't need the extra power.
Courtesy : Internet
public enum Suit { clubs, diamonds, hearts, spades }Enum has various advantages :
- Compile-time type safety.
- Performance comparable to int constants.
- Type system provides a namespace for each enum type, so you don't have to prefix each constant name.
- Typesafe constants aren't compiled into clients, so you can add, reorder or even remove constants without the need to recompile clients. (If you remove a constant that a client is using, you'll fail fast with an informative error message.)
- Printed values are informative. (Which would you rather see in a stack trace: "Indigo" or "6?")
- Enum constants can be used in collections (e.g., as HashMap keys).
- You can add arbitrary fields and methods to an enum class.
- An enum type can be made to implement arbitrary interfaces.
- The proposed construct is simple and readable. (The Typesafe Enum pattern described in "Effective Java" involves a large amount of boilerplate code, which obscures programmer intent, deters all but the most determined programmers from using the pattern, and provides opportunities to introduce errors.)
- The proposed construct can be used with switch statements.
Arbitrary fields may be added to enum classes, and to individual enum constants. The ability to add such fields will only be used by relatively sophisticated programmers, but greatly enhances the power of the facility. It adds nothing to the complexity of the facility for programmers who don't need the extra power.
Courtesy : Internet
No comments:
Post a Comment