I want to read a property for config file and assign it to a static final
variable and if in case the config file is omit/or not exists, using default value hard-coded.
public static final String NOTIFY_ALI;
static {
try {
PropertyReader notifyConf = new PropertyReader("notify.conf");
NOTIFY_ALI = notifyConf.getProperty("notify_ali","http://notify.foo.com/notify");
} catch (IOException e) {
e.printStackTrace();
NOTIFY_ALI = "http://notify.foo.com/notify";
}
}
NOTIFY_ALI
should be assigned by the configure file notify.conf
with the key notify_ali
or if it's not explicit in the file, take http://notify.foo.com/notify
as a default value. And if the config file is not exists(IOException
will occur), just catch the exception and assign with the default value as well.
But the upper code snippet give a compile time Err:
Error:(18, 13) java: variable NOTIFY_ALI might already have been assigned
Can I do this?
Create a method that returns the URL and use to assign it when is declared
If you need to initialize more than one variable you could do it like this