Where to store global variables like file paths in java?

In my application, I use several icons. Where should I store the path to the directory containing these icons?

Icons are used in different classes, so there really is no point in storing them in one of these classes in particular.

I read that global variables are evil, but is it acceptable to use a class (e.g. Commons ) containing only public static final fields to save this data king? What solution is used in professional applications?

+7
java global-variables
source share
6 answers

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? ]
+7
source share

Global variables are evil (because they make it almost impossible to figure out who modifies them), but constants are not evil. The public static final String fields are beautiful because they cannot be changed.

+4
source share

I would recommend including them (icons) with your class files in the bank, for example, in a folder called resources, and only the icon loader should know the name of the resource folder in your bank.

+3
source share

You mean constants, not global variables, so don’t worry that they are evil - they are not, because they do not change.

  • if they are used by one class - put them in this class
  • if they are used by several classes in one package - put them in a special class
  • if they are used by several classes and they logically belong somewhere, put them there.

Keep in mind that if these "constants" are actually configurable, you better pass the Configuration object to the methods that need it. Well, you might have static somewhere, but from the point of view of verification, you must definitely enter / transmit them.

+2
source share

Global variables do not match global constants. The reason global variables are bad is because they can be changed anywhere in the code, and it is very difficult to track errors that occur due to the absence of a global variable in the expected state. Global constants will always be in the expected state, because they can never be inadvertently changed.

+1
source share

In general, I would suggest that this particular case is a packaging problem and does not refer to elements as files in the file system, but rather as elements in the class path and loads them through the class loader. This requires setting their location in the class path of your application.

Then there should be only one class that knows how to retrieve these icons, and all other code requests this class for the icons that it needs.

0
source share

All Articles