The singleton should be initialized with a parameter. I want to use a static block to obtain the parameter from system properties instead of a setter method. I don't want to use setter because this singleton may be used concurrently, and I don't want to make the setter synchronized. I don't want to use constructor because I use enum to make it singleton and enum can not define instances with runtime parameter.
Is there any reason that I should use the setter instead of system properties? I know it means another thing to watch out for starting the java application, which is not very program-like. Thanks.
You are right. System properties are static, so why do not make static holders for them? as example instead of getting file.separator or line.sparator from System.getProperty each time I need them I can have them like this:
public class MyConstants {
public static final String FILE_SEPARATOR = System.getProperty("file.separator");
public static final String LINE_SEPARATOR = System.getProperty("line.separator");
and then use them as MyConstants.FILE_SEPARATOR or MyConstants.LINE_SEPARATOR
It is just a matter of code style you prefer. In opposite: non-static setters or constructors with parameters required when you intend to have different instances of the same class with different values in them. Static setters are not often required... just sometime when frameworks like Spring is in use and/or actual values for the class level attributes are not in System properties, but somewhere in kind of .properties files