Properties file vs Constants class in Java

前端 未结 8 1243
臣服心动
臣服心动 2020-12-16 11:03

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

8条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-16 11:27

    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.

提交回复
热议问题