4

When it comes to declare predefined constants in a name-value pair, I have been randomly choosing between 'java.util.Properties', 'enums' or a separate class with 'public static final' values.

For future reference, I need some guidelines over which approach to take.

Thanks!

stacker
  • 68,052
  • 28
  • 140
  • 210
dev
  • 11,071
  • 22
  • 74
  • 122
  • I personally have a class file with public static final values. I find it an easy go to point and I know where to look. If you start putting enums or anything in specific classes, now you have to remember which class it is in. As far as performance goes, I don't know. I'm sure someone else will be able to answer that. – Green Day Jan 14 '12 at 19:24

4 Answers4

4

It all depends of your constant lifecycle. Constant are by definition something that do not move. Choosing the right methods will be a question of the likely to change and the repackaging need.

  • If you're really sure, it wont move ever : static final is the way to go. Pi, mathematical constants, things like that are a good example.

  • If you think there is a potential change but need the ease of code manipulation and do not fear of ascendant compatibility, enums is ok. I did that for error code some time ago.

  • If you think there is a potential change but you don't want this change to impact your code, properties (with resource bundle) is the better choice. Labels (translations), inital settings, etc are also a good example.

Gomoku7
  • 1,362
  • 13
  • 31
  • 1
    @Raj Use enums for a constant set e.g. in card games you have Spades ,Clubs, Hearts and Diamonds. Another case could be supported file-types PNG,GIF etc. there would be no drawback if you add later BMP since then you would have another API version including an enum that matches the new implementation. – stacker Jan 14 '12 at 19:52
  • Very thorough answer. +1. Raj: you should also take note of @Sean Reilly's comment on my answer. – Milad Naseri Jan 14 '12 at 19:55
2

static final fields are used when you cannot form a definite set of closed options from which you can choose the state of a variable. On the contrary, when you can, you always use enums.

Now, when you want to keep a dictionary of key-values, regardless of their nature, it's time to use a Properties type object or sometimes a Map.

Milad Naseri
  • 4,053
  • 1
  • 27
  • 39
  • 5
    If you need key-value pairs, and the keys are set of closed options, then consider combining the enum and a map approaches by using java.util.EnumMap – Sean Reilly Jan 14 '12 at 19:36
  • Indeed. Also, note that if you are using constants and not *really* values from a closed set with a domain-specific meaning, `static final` s are the better way. In addition, if you are using the constants only within your own class, consider changing the `public` modifier to `private`. – Milad Naseri Jan 14 '12 at 19:38
0

Use Enums when your set of constants are fixed and not expected to change often. If it often changes then its difficult to maintain backward compatibility with your previous versions. If in a client server architecture both have different versions of some Enum. E.g

Server : public enum Priority{ HIGH,LOW,MEDIUM,AVERAGE}

Client : public enum Priority{ HIGH,LOW,MEDIUM}

Let's say if server sends Priority.AVERAGE to client then the client will throw exception.

sumit dugar
  • 121
  • 1
  • 6
0

One more thing to consider - will these Strings change in different versions? i.e. might you have a French version, a Chinese version, an "advanced" version? If so, Properties / ResourceBundles etc. are the way to go.

user949300
  • 15,364
  • 7
  • 35
  • 66