35

18 Points Every Java Developer Should Know About Enums In Java

 5 years ago
source link: https://www.tuicool.com/articles/hit/6NFnIrB
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

In the following post, we will take a look at the top 18 tips for working with Java enums. Let's get into it!

  1. Even though it is not mandatory to declare enum constants with UPPERCASE letters, it is in the best practice to do so.

  2. Enum types, like classes, can have fields , constructors , and methods along with enum constants.

  3. Enum constructors are private by default. Only private constructors are allowed in enum types. That’s why you can’t instantiate enum types using a new operator.

  4. Enum constants are created only once for the whole execution. All enum constants are created when you initially refer any enum constant in your code. While creating each enum constant, the corresponding constructor is called.

  5. Enum constants must be declared before fields, constructors, and methods (if any).

  6. All enum types extend the java.lang.Enum class by default. As multiple inheritances are not supported in Java, they can’t extend to any other classes.

  7. Enum types can implement any number of interfaces.

  8. Enum constants can have their own body called Constant Specific Body . In that body, you can define fields and methods. But, these methods and fields are visible within the Constant Specific Body in which they were defined.

  9. Enum types are final by default. They cannot be extended by any other types.

  10. For every enum type written in a file, the .class file will be generated after compilation.

  11. Enum types can have any number of static initialization blocks as well as instance initialization blocks .

  12. Since the java.lang.Enum class implements the Comparable and  Serializable interface, all enum types are Comparable and Serializable by default.

  13. We can compare the enum constants using the == operator.

  14. You can retrieve the enum constants of any enum type using the values() method. The  values() method returns an array of enum constants.

  15. Enums provide type-safety during compilation. That means you will get a compile-time error if you try to assign any values other than the specified enum constants.

  16. You can define enum types outside a class or inside a class but not inside a method or block.

  17. The ordinal() method is used to get the order of an enum constant in an enum type.

  18. Enums are mostly used when you want to allow a limited set of options that remain constant for the whole execution and you know all possible options at compile time. For example, this could be choices on a menu or options of a combobox .

See more at https://javaconceptoftheday.com/18-points-every-java-developer-should-know-about-enums-in-java/

Further Reading on Java Enums


About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK