Global constants
According to others, global constants do not have the same negative connotation as global variables. Global variables make debugging and program maintenance difficult due to uncontrolled changes. Global constants ( public static final ) do not pose the same problem.
However, object orientation is about binding code that is close to its data to increase clarity and maintainability. You still need to find the right balance between storing global configuration values in a global class and storing data close to the code used by it.
It is probably worth mentioning that since the compiler can embed some constants, if you change the constant value, you may have to recompile and redistribute more than just a class containing constants.
External values
You also asked about what professional applications do. This is not uncommon for applications that make these types of values, such as file paths, externally customizable. It depends on how likely the change in value is (for example, how likely it is that your application will move or your code will be used in another application), and how convenient or simple it is to recompile and redeploy the code with the new values. If you decide to make some values customizable from the outside, you might still need to encode the default values for these elements in your code.
Here are some ways to externalize these values and some links to get you started. This is, of course, not an exhaustive list:
- System properties so you can specify them on the command line
- Property Files [See StackOverflow Q - How to use java property files? ]
- Resource Links [See StackOverflow Q - How to download a resource bundle from a file resource? ]
Bert f
source share