Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Static String constants VS enum in Java 5+

Tags:

I've read that question & answers: What is the best way to implement constants in Java?

And came up with a decision that enum is better way to implement a set of constants. Also, I've read an example on Sun web site how to add the behaviour to enum (see the link in the previously mentioned post). So there's no problem in adding the constructor with a String key to the enum to hold a bunch of String values.

The single problem here is that we need to add ".nameOfProperty" to get access to the String value. So everywhere in the code we need to address to the constant value not only by it's name (EnumName.MY_CONSTANT), but like that (Enum.MY_CONSTANT.propertyName).

Am I right here? What do you think of it?

like image 911
EugeneP Avatar asked Dec 07 '09 09:12

EugeneP


People also ask

Which is better enum or constant in Java?

Difference between Enums and Classes The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).

Should I use enum instead of String constants?

I would advice you to use enums only if you really need enumerated constants, or some additional functionality common for all items. That's of course depending on the type of application you are writing and what versions and devices you want to support.

What is difference between constant and enum in Java?

With a set of constants, any value of the same intrinsic type could be used, introducing errors. With an enum only the applicable values can be used.

What is the difference between enum and const?

Solution 1. No, enum is a type that defines named values, a constant variable or value can be any type. You use an enum variable to hold a value of the enum type, you use a const to define a variable that cannot change and whose value is known at compile time.


1 Answers

Yes, the naming may seem a bit longer. But not as much as one could imagine...

  1. Because the enum class already give some context ("What is the set of constants that this belong to?"), the instance name is usually shorter that the constant name (strong typing already discriminated from similar named instances in other enums).

  2. Also, you can use static imports to further reduce the length. You shouldn't use it everywhere, to avoid confusions, but I feel that a code that is strongly linked to the enum can be fine with it.

  3. In switches on the enum, you don't use the class name. (Switches are not even possible on Strings pre Java 7.)

  4. In the enum class itself, you use the short names.

  5. Because enums have methods, many low-level codes that would make heavy use of the constants could migrate from a business code to the enum class itself (either dynamic or static method). As we saw, migrating code to the enum reduces the long names uses even further.

  6. Constants are often treated in groups, such as an if that test for equality with one of six constants, or four others etc. Enums are equipped with EnumSets with a contains method (or similarly a dynamic method that returns the appropriate group), that allow you to treat a group as a group (as a secondary advantage, note that these two implementations of the grouping are extraordinarily fast - O(1) - and low on memory!).

With all these points, I found out that the actual codes are much much shorter !

like image 187
KLE Avatar answered Nov 12 '22 02:11

KLE