This question extends Initialize final variable before constructor in Java as I was not satisfied with the answer provided there.
I have the same question. I have variables that I need to be set as final
but I cannot do so because I need to set them to values that require exceptions to be caught thus making it impossible unless I put them in the constructor. The problem with that is that I then have to make a new instance of the object every time I want to reference the final
static
variables which doesn't really make sense...
An example where path
cannot be defined outside of the constructor nor inside the constructor unless a new
instance is created each time the object is referenced from a different class:
public class Configuration {
private static final String path;
public Configuration() throws IOException, URISyntaxException {
propertiesUtility = new PropertiesUtility();
path = propertiesUtility.readProperty("path");
}
}