Which to use Properties file or java file with constants
Hi all, I have a small problem which I could not get anything on Google.
I have some attributes which are used in a java program, now these constants can vary depending on certain rare conditions and maybe not at all. So the thing is should I add these as constants in a java file or make a key value pair entry using a class(java file).
Can anyone explain which is better and also which to use when, or provide some links.
this would be really helpful
Thanks in advance
[502 byte] By [
Austina] at [2007-11-27 9:38:44]

If they're pretty much constants, but may occasionally change, keep 'em in a properties file, and use that properties file to populate a bunch of static fields in a class, eg
public class AlmostConstants {
private static Properties props;
static {
props = new Properties();
try {
props.load(AlmostConstants.class.getResourceAsStream("props.properties"));
} catch (IOException e) {
e.printStackTrace();
}
}
public static final String PROPERTY = props.getProperty("property");
public static void main(String[] args) {
System.err.println(PROPERTY);
}
}
Tends to depend just how constant your constants are. If they vary between installations, but never once installed, then you should probably us a Properties file. For slightly more volatile ones then you should take a look at the Preferences class (which stores values in the registry under Windows).
For constants that change for special runs, then use System.getProperty() and -D flags on the java command.
>If they're pretty much constants, but may occasionally change, >keep 'em in a properties file, and use that properties file to populate a >bunch of static fields in a class, eg
This would mean that this class will have to be called whenever i change the property file,.
one more thing, is there more overhead when reading from a properties file than from a .class file.
> >If they're pretty much constants, but may
> occasionally change, >keep 'em in a properties file,
> and use that properties file to populate a >bunch of
> static fields in a class, eg
>
> This would mean that this class will have to be
> called whenever i change the property file,.
Re-initialized, yes. But how often are you envisaging these values changing? If, as you said, it's hardly ever, it shouldn't be a problem. If you see them changing during a single run of the program, it's probably not appropriate to use the code I posted
> one more thing, is there more overhead when reading
> from a properties file than from a .class file.
Yes, but don't worry about it. You won't notice
BTW be warned about putting static final constants in some Constants class. static final constants that are either String, or primitive types actually get compiled into classes that reference them as values, not references. If you change them you need to be sure the referring class is recompiled, or the change is likely not to be effective.
Most of my programs have some kind of configuration file, for more complex programs I tend to have an XML config file.