I am a bit confused between using a constants file and properties file in Java.
How to decide when to use Constants.java and when to use a .properties file?
The advantage of using properties file is we can configure things which are prone to change over a period of time without the need of changing anything in code. Properties file provide flexibility in terms of configuration. Sample properties file is shown below, which has information in key-value pair.
A constant is a variable whose value cannot change once it has been assigned. Java doesn't have built-in support for constants. A constant can make our program more easily read and understood by others. In addition, a constant is cached by the JVM as well as our application, so using a constant can improve performance.
properties is a file extension for files mainly used in Java-related technologies to store the configurable parameters of an application. They can also be used for storing strings for Internationalization and localization; these are known as Property Resource Bundles.
In Plain Java A properties file consists of key-value pairs of string type. We can write values to a properties file in plain Java using the Properties class.
Use hardwired constants in your Java code when you don't want users / deployers / testers / tests changing them.
Use a properties file when you do want this to be a possibility.
The point is that changing a hard-wired constant in your application's source code entails editing the source code, rebuilding and redeploying. By contrast, changing a properties file may be as simple as firing up NotePad.
You commented:
As you said that changing properties file is simple whereas changing constants file requires us to rebuild the application. So, shouldn't we always prefer to use properties file?
No. Not always. For example, if you distribute your application to end users to install on their machines and it has constants that you do not want users to change it would be a bad idea to put them in a properties file.
It is impossible to reduce this to "always prefer X" recommendation. You need to understand your own application requirements and decide for yourself.
My check list
Property file:
Is it configurable per environemnt etc.
Messages, labels etc.
Applicable to particular situation (list of states for rules etc). key-value pairs. Can be modified by someone other than developer ie, analysts, business users etc.
Constant:
Are constants. Not configurable. Mostly for optimization and reuse. To avoid keys being scattered.
For constants like YES = "yes". Not really a key value. Keys for cache etc.
Constants to ensure that retrieval and set use the same key even though from different places in the application, EXAMPLE xyz.put(KeyConstants.SOME_KEY, "somevalue"); xyz.get(KeyConstants.SOME_KEY) from different classes, ofcouse xyz being shared or singleton.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With