Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where to store global variables like file paths in java?

In my application I use some icons. Where should I store the path of the directory containing those icons ?

The icons are used in different classes so it doesn't really make sense to store them in one of those classes in particular.

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

like image 247
Jules Olléon Avatar asked Apr 11 '10 15:04

Jules Olléon


People also ask

Where are global variables stored in Java?

Global variables are stored in the data segment of memory. Local variables are stored in a stack in memory.

How do you add a file path to a string in Java?

In order to build your Paths, you can use something like this: documentsPath = Paths. get(string. format("C:\\Users\\%s\\%s", username, "Documents");

What is directory path in Java?

A Java Path instance represents a path in the file system. A path can point to either a file or a directory. A path can be absolute or relative. An absolute path contains the full path from the root of the file system down to the file or directory it points to.

Can we declare variable anywhere in Java?

We can declare class variables anywhere in class, but outside methods. Access specified of member variables doesn't affect scope of them within a class.


2 Answers

Global Constants

As others state, global constants don't have the same negative connotation as global variables. Global variables make a program difficult to debug and maintain because of uncontrolled modifications. Global constants (public static final) don't create the same problem

Nevertheless, object-orientation is about binding code close to its data to enhance understandability and maintainability. You still have to find the right balance between storing global configuration values in a global class vs keeping data close to the code that will use it.

It is probably also worth reminding here that, because the compiler may inline some constants, if you change a constant value, you may have to recompile and redeploy more than just the class that contains the constants.

Externalizing Values

You also asked about what professional apps do. Its not uncommon for those apps to make these types of values, like files paths, externally configurable. It depends on how likely the value is to change (i.e. how likely your app will move or your code will be used in another app) and how convenient or easy it is to recompile and redeploy the code with new values. If you do choose to make some values externally configurable, you still may want to encode default values for those items in the code.

Here are some ways to externalize those 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 Bundles [See StackOverflow Q - How to load a resource bundle from a file resource?]
like image 152
Bert F Avatar answered Nov 12 '22 13:11

Bert F


Global variables are evil (since they make it nearly impossible to figure out who modifies what), but constants aren't evil. public static final String fields are fine, since they can't be modified.

like image 28
gustafc Avatar answered Nov 12 '22 12:11

gustafc