Search code examples
javastringconstantsresourcebundle

String Constants Vs Resource Bundle in an Java web Application


From past Year are so, we have developed an application, in which we have used the static String Constants to store the Constants. Like public static final String PAYMENT_CHEQUE = "cheque"; Where Ever I Require i.e. in the jsp pages as well the Action class I will be refering to the Above String Constant I am thinking after reviewing the Resource Bundle, properties Files, My Question is

  1. Is there any performance hit if I use the properties file compared to Static String Constant?
  2. Which is better Static string constant and Properties File Key Value pair?
  3. Is that good idea id we use the static String Constant(For Labeling) in the jsp?

Please suggest me


Solution

  • In general, using hard-coded strings is a bad idea, because it means every change needs a new compilation-deployment cycle, whereas using a properties file means the application needs to be restarted and nothing else (although some would say that in itself is a high price to pay). Another advantage of using a properties file is the ability to switch languages by switching the application to another file - much more reasonable than having multiple sets of constant labels in code.

    You can use the final static String fields the same way you do right now, only give them a value read fro ma properties file at the application's initialization stage, rather than going back to the file for every constant (which would be costly!). That way you don't have to make application-wide changes, and you still enjoy the advantages I mentioned above.