Use properties file instead of static final variables

后端 未结 6 1499
轮回少年
轮回少年 2021-02-08 03:05

I have many classes with static final fields which are used as default values or config. What is best approach to create global config file? Should I move these fields to single

6条回答
  •  后悔当初
    2021-02-08 03:45

    The answer depends...

    • Put stuff in properties files if the values change depending on runtime environment (eg database connection settings, foreign server IPs), or that will likely change often/soon
    • Prefer using enum to static final constants where possible (avoid "stringly typed" code)
    • Find an existing library that might have what you want (eg use TimeUnit to convert hours to seconds, rather than having static final int SECONDS_IN_HOUR = 3600;)
    • What's left (that hopefully isn't going to change any time soon), use public static final in the class that has "most ownership" over them
    • Avoid classes that have static methods that return a constant - it's just code bloat

提交回复
热议问题