Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Use properties file instead of static final variables

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 static class, use properties file or what?

Edit: I need to use this values both in java classes and xhtml pages. Values dosen't depend on envirnoment. I could compile project to set new values - no problem.

like image 510
karolkpl Avatar asked Jan 18 '12 13:01

karolkpl


1 Answers

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
like image 166
Bohemian Avatar answered Sep 28 '22 06:09

Bohemian