I've been working on a small Java project by myself for a department that does not do a lot of software development but mostly database stuff.

I showed my boss the code I've been writing and he saw that in some classes there were some static variables. He then told me to put the static variables in XML instead to make it "easier."

He left and I did not get to ask him how it makes them manageable. He's going to be gone for a couple of weeks.

I know this might be a pretty vague question but what could possibly be the reasons why he would tell me to put static variables into XML? And how do I go about doing them?

First, I had something like this:

private final static String[] ACCESS_EXTENSIONS = {"accdb", "mdb"};

After he told me about putting static variables in an XML file, I wrote an XML code:


    <filetype id = "access">
        <title>Access Database</title>

    <filetype id = ....>



But then this resulted to me creating a static String variable that contains the location of my XML file:

private final static String xmlFile = "location_of_my_xml_file";

I also learned about how to use XPath to access data in my XML, but I ended up writing another static code which is:

private final static String GET_ACCESS_EXTNS_EXP =

Now I think I made things worse. I know I should wait for him to get back to clarify, but that is a long time from now, and I think it's best if I start reading up while waiting. I just need ideas on where to start.

  • recommended reading: Where to start?
    – gnat
    Commented Jan 26, 2016 at 15:35
  • You would put such variables in an XML file to make them configurable. In other words, you can change their values without recompiling the program. Commented Jan 26, 2016 at 15:37
  • @Robert Harvey Hi, thanks. I think this sounds correct. What do I need to look up for so I can learn more about this?
    – Wabbage
    Commented Jan 26, 2016 at 15:40

2 Answers 2


Storing the configuration of the program externally allows you to avoid needing to recompile the program when something changes. Switch from databaseA to databaseB? Modify the property file rather than modify the hard coded values and recompile from source.

What's more, Java offers a class as part of its standard runtime environment that facilitates this - the Properties class. This class can either take a .properties file or an .xml file that matches the dtd specified at http://java.sun.com/dtd/properties.dtd (it really is a rather simple one - its the same as the one in the javadoc).

With the Properties class, you load it in, and then you can easily call getProperty(String key) or getProperty(String key, String defaultValue) and get your values out of it. This is much easier than loading values from a custom xml file and all the xml or xpath processing that you would have to do with it. Furthermore, its a known thing and easier to get right.

That's the property file.

access.extensions = accdb,mdb # where you might find an access database

And then you've got the corresponding bit of Java...

String[] extensions = prop.getproperty("access.extensions", "accdb").split(",");

for reading it out. This makes the code a bit more self documenting, a safeguard against not having the file with a default value.

If you really wanted to use xml instead of a properties file, it would look like:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd">
  <comment>Access Database Extensions</comment>
  <entry key="access.extensions">accdb,mdb</entry>

But again, the key point is to move the values out of the code and into external files that are easier to work with. And Java already has the tools for reading those back into the system.

  • I stuck with XML and this is what I did, and works great! Thanks!
    – Wabbage
    Commented Feb 2, 2016 at 14:30

You would put such variables in an XML file to make them configurable. In other words, you can change their values without recompiling the program.

Further Reading
XML configuration files
How to easily load an XML based configuration file into a Java class


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.