Properties file vs Constants class in Java

前端 未结 8 1198
臣服心动
臣服心动 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:44

    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.

    0 讨论(0)
  • 2020-12-16 11:45

    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.

    0 讨论(0)
提交回复
热议问题