15

I have been reading a lot of posts on this site regarding the usage of constants.

Question: When should I use Enums for constants, vs using classes or interfaces.

I see 2 key situations I am looking to address.

1. Global Constants used in a applications by multiple projects.

Example:

  • Common logging strings
  • Container references like a database mapping reference used in WebSphere EAR's

2. Object Specific Constants

Example:

  • Employee pay rates for an Employee Object

From everything I have read this is what I think I have a grasp on and what I am looking for an opinion on.

For situation 1: Design Approach: Use a final class and a static import.
Seen here: What is the use of interface constants?

For Situation 2: Design Approach: Apply the use of Enums to represent those constants as a object.

Additional points to remember:

  • If the constant string belongs to the class and you only need the string value keep in the class that uses it
  • Don't use an Interface for situation 1. As mentioned in the link above as Constant Interface Anti-pattern. .

Thanks in advance for thoughts and opinions.

Community
  • 1
  • 1
haju
  • 1,278
  • 4
  • 20
  • 38
  • 1
    Strings are already immutable, so there is little need to store them in enums. Page names does not seem to be too static, and since there is no way to extend enums during runtime, you might be better off by strings. Data base references do seem to be more or less static, but I presume that they are normally represented by strings as well. There seems to be no direct need to enumerate them. Make sure you design an application with enum as an option, not as a requirement. – Maarten Bodewes May 26 '11 at 20:01

3 Answers3

12

Global constants as you put it should actually be in a properties file as it allows each application to configure them individually without a code modification. For object specific constants my general rule of thumb on Enum versus static final I typically lean towards how many elements there are to have and how related those elements are. If there is a big relation between them such as Suits in a deck of Cards then I would go for the enum. If it is default age for a user, then this becomes a final as there is no purpose to making it an enum as it would not need to be referenced in many areas. These are just some thoughts on each of the ways I have approached it.

Woot4Moo
  • 23,987
  • 16
  • 94
  • 151
  • That's a good point about the database or configuration file. I should of been more aware of that, reading to many posts on this. That would be the approach to global static strings that affect application logic, however would not be used to store static strings like used in logging. I still think a static class import would be better wouldn't you think? For example logging statements that is commonly used like "Enter method" or "Exit Method" that you add for debugging. – haju May 26 '11 at 22:43
  • @haju those could still be in the properties file. The reason for that is the logger still has to construct the log statement so constants wouldn't help performance. The properties file also makes it easier if you ever change to a different language such as Japanese or C#. – Woot4Moo May 26 '11 at 22:53
  • I don't think the language of the comments would change unless the code would be bought by another company lol I think then there would be more things to worry about lol. To me the properties file for something like that, and having load them through a PropertyManager to persist it to other classes. I prefer the simplicity of having a class with static import. All preference in this case. – haju May 26 '11 at 23:35
3
  1. Global constants used by different projects: Enum
    Better to use Enum over public static final members in a class. More clean and easy to understand I guess.

  2. Object specific constants: public static final members in Class. Because, they are needed only within the scope of the object, then no need to created a new Enum for that.

Nice read

Update (fixed broken link):

  1. Making the Most of Java 5.0: Enum Tricks
  2. Making the Most of Java 5.0: Enum Example
zengr
  • 38,346
  • 37
  • 130
  • 192
  • I sort of half disagree with this. Maybe I didn't clearly state what I mean't. I agree with the other posters, I should of thought of it more.If its a global constant that is static and affects application logic data,it should be represented in database or config file. However if its a object specific constant that is a a one time use, I would use like you said a static final member in the class. Otherwise I would look to using an Enum to refactor the objects constants. – haju May 26 '11 at 22:40
0

It sounds like almost everything you've listed for both numbers 1 and 2 belong in configuration files or database tables.

Do you want to re-compile code when your employee's get a raise or a page name changes?

Unless there is a compelling reason everything else that is constant should be modeled as an enum. This way you realize the benefits of fast object equality comparisons and you avoid problems associated String constants.

The scope of those enums however is application specific. If the enumeration is only used by a class that it should be a private enum. If it is shared by multiple classes then it should be in its own class definition file.

nsfyn55
  • 14,875
  • 8
  • 50
  • 77
  • Yes you are right like Woot4Moo comment. However the one case this would not be true would be for common logging statements as I stated in my comment @Woot4Moo above. – haju May 26 '11 at 22:48
  • What is the real purpose of a common logging statement? I've written a lot of lines of code and not found the need for common logging statements. But I don't know your situation so I'll try not to judge. I would still argue that these belong in a configuration or properties file such that they can be modified/internationalized without requiring code edits plus gain the benefits of property substitution by leveraging the appropriate frameworks. Constants classes destroy OO code by forcing an unnatural static dependency between every class in the system and the constants class – nsfyn55 May 27 '11 at 18:06