I was wondering whether it is possible to call a static method from a static block in order to initialize static variables. Is e.g. the following possible:
public class AppProperties {
private static final Logger logger = LoggerFactory.getLogger(AppProperties.class);
private static final String PARSER_PROPERTIES_FILE = "/parser.properties";
private static final Properties PARSER_PROPERTIES = new Properties();
private static final Properties DAO_PROPERTIES = new Properties();
static {
loadParserProperties();
loadDaoProperties();
// Some other configuration
}
public static void loadParserProperties() {
// Loading parser properties
}
// Further methods omitted
}
Is it good practice?
EDIT: Oracle recommends initialization as follows:
class Whatever {
public static VarType myVar = initializeClassVariable();
private static VarType initializeClassVariable() {
// Initialization code goes here
}
}
Their explanation is:
The advantage of private static methods is that they can be reused later if you need to reinitialize the class variable.
However, the AppProperties
code is also reusable. I have a feeling that I am missing something. Calling static methods from static blocks isn't mentioned, that's why I assume it is bad practice.