Anand
Anand

Reputation: 21320

Properties file vs Constants class in Java

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?

Upvotes: 29

Views: 15632

Answers (8)

Stephen C
Stephen C

Reputation: 718906

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 an "always prefer X" recommendation. You need to understand your own application requirements and decide for yourself.

Upvotes: 38

ftr
ftr

Reputation: 2145

You usually use property files when you want to specify parameters that vary from deployment to deployment or over time.

You use constants when the parameters are not dynamic and thus not supposed to be changed depending on external factors. You have to recompile the class every time you change a value.

Upvotes: 1

techuser soma
techuser soma

Reputation: 4877

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.

Upvotes: 5

SantoshK
SantoshK

Reputation: 96

Properties have the advantage of being externalizable. You can have one property file for development, another for test and another for production. A lot of times, though, rarely changed properties, that are internal to the workings of the software are put into properties file making them hard to manage.

OTOH constants are compiled so any errors will be caught at compile time.

So the answer is , it depends. If the values in the properties are truly configuration changes you want to externalize then use properties file. else make them Java constants. You might end up using both, though.

Upvotes: 0

fastcodejava
fastcodejava

Reputation: 41097

Constants are fixed in compile time. So if you don't foresee any changes of values Constants will be good idea.

Upvotes: 1

javatutorial
javatutorial

Reputation: 1944

There are many different aspects to consider. The most important are that

  • you can edit properties files and use it without re-compiling your application;
  • properties files contain no code, so everybody can change them without any Java knowledge.

These aspects make properties file really useful for storing configurations.

On the other hand, constants classes need to be compiled, but are "safer" if you plan not to change those constants very often.

Upvotes: 1

Peter
Peter

Reputation: 6362

Generally, anything in a constants class is considered to be "hardcoded"; that is, you are required to re-compile to make changes.

Use a .properties file for things like configuration, where you don't have to be forced to re-compile just to make changes. In this way, you can simply change the properties file and restart your application.

Upvotes: 1

adarshr
adarshr

Reputation: 62593

  • Constants - when you don't mind re-compiling the application each time you change value. Sort of an irony here. Why would you change something if it has been called a constant :)

  • Properties file - when you want the luxury of just changing the value and maybe restarting the application to pick up the change.

Upvotes: 1

Related Questions