Reading of named settings from the XML Usually it is a good style in Java to create a named constant for a string (and other) hardcoded values. Using the example below I want to know how pedantic I should be in it.

I use a Java class as a wrapper of XML settings. Each function is suppose to return a value from a related XML file.

I defined a lot of constants with keys names on the top and each function use one single constant. Is it a good idea to remove constants and use hardcoded strings directly in each function?

Here is my settings file:

package com.art.backend.configs;

import com.art.backend.configs.inherited.Config;

public class ConfigStorage extends Config {

    private static final String SETTINGS_FILENAME = "storage.cfg.xml";

    private static final String KEY_HOST = "host";
    private static final String KEY_PORT = "port";
    private static final String KEY_USERNAME = "username";
    private static final String KEY_PASSWORD = "password";
    private static final String KEY_CONNECTION_TYPE = "connection_type";
    private static final String KEY_SEPARATOR= "separator";
    private static final String KEY_REMOTE_ROOT = "remote_root";
    private static final String KEY_SUBDIR_UPLOADED = "subdir_uploaded";

    public ConfigStorage() {
        super(SETTINGS_FILENAME);
    }

    public String getHost(){ return getString(KEY_HOST); }

    public int getPort(){ return getInt(KEY_PORT); }

    public String getUsername(){ return getString(KEY_USERNAME); }

    public String getPassword(){ return getString(KEY_PASSWORD); }

    public String getSeparator(){ return getString(KEY_SEPARATOR); }

    public String getConnectionType(){ return getString(KEY_CONNECTION_TYPE); }

    public String getRemoteRoot(){ return getString(KEY_REMOTE_ROOT); }

    public String getSubdirUploaded(){ return getString(KEY_SUBDIR_UPLOADED); }
}


Also I will attach file "Config" as a base class for my settings and XML settings file just in case, but they are not really important.

package com.art.backend.configs.inherited;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public abstract class Config {
    private static final String MSG_SETTING_NOT_FOUNT = "Settings file not found";

    private final String filename;
    private Properties properties = null;

    public Config(String filename){
        this.filename = filename;
        init();
    }

    private void init(){

        //Reading properties file in Java example
        try {
            properties = new Properties();
            InputStream fis = getClass().getClassLoader().getResourceAsStream("storage.cfg.xml");
            properties.loadFromXML(fis);
        } catch (IOException e) {
            e.printStackTrace();
            throw new IllegalArgumentException(MSG_SETTING_NOT_FOUNT);
        }
    }

    public String getFilename(){ return filename; }

    public String getString(String key){
        return properties.getProperty(key);
    }

    public int getInt(String key){
        return Integer.parseInt(properties.getProperty(key));
    }

    public float getFloat(String key){
        return Float.parseFloat(properties.getProperty(key));
    }

    public boolean getBoolean(String key){
        return Boolean.parseBoolean(properties.getProperty(key));
    }
}


XML settings

&lt;?xml version="1.0" encoding="UTF-8" standalone="no"?&gt;
&lt;!DOCTYPE properties SYSTEM "http://java.sun.com/dtd/properties.dtd"&gt;
&lt;properties&gt;
    &lt;entry key="host"&gt;***&lt;/entry&gt;
    &lt;entry key="port"&gt;***&lt;/entry&gt;
    &lt;entry key="username"&gt;***&lt;/entry&gt;
    &lt;entry key="password"&gt;***&lt;/entry&gt;
    &lt;entry key="connection_type"&gt;sftp&lt;/entry&gt;
    &lt;entry key="separator"&gt;/&lt;/entry&gt;
    &lt;entry key="remote_root"&gt;/home/adzhus/art_storage/&lt;/entry&gt;
    &lt;entry key="subdir_uploaded"&gt;uploaded/&lt;/entry&gt;
&lt;/properties&gt;
In your use case, where the constants are private and only ever used in one place, they are pretty much boilerplate. One could argue that the constants group the keys into one condense place where they can be referred to easily when reading code.