I’m a begginner java programmer; now I’m using J2SE and NetBeans 6.9.1 to code an application.
The issue I’m facing now is to load properties from a well known location in my local filesystem using a singleton class. What annoys me is that I get an error which is claiming for a throws clause or catch the exception where the INSTANCE variable is being initialized. May anyone help me undestand this.
Thanks in advance.
The code is as follows :
package cat.oai.atapplications.phot;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.Properties;
/**
*
* @author fdalmau
*/
public class PhotPropertiesManager {
private static Properties photProperties;
private PhotPropertiesManager() throws FileNotFoundException, IOException {
photProperties = LoadProperties();
}
public static PhotPropertiesManager getInstance() {
return PhotPropertiesManagerHolder.INSTANCE;
}
public static Properties getPhotProperties() {
return PhotPropertiesManager.photProperties;
}
private static Properties LoadProperties() throws FileNotFoundException,
IOException {
Properties defaultPhotProperties = new Properties();
FileInputStream in = new FileInputStream("defaultphot");
defaultPhotProperties.load(in);
in.close();
Properties applicationPhotProperties =
new Properties(defaultPhotProperties);
in = new FileInputStream("lastexecutionphot");
applicationPhotProperties.load(in);
in.close();
return applicationPhotProperties;
}
private static class PhotPropertiesManagerHolder {
/******
The problem is is this line of code:
******/
static final PhotPropertiesManager INSTANCE = new PhotPropertiesManager();
}
}
The problem is that a static field cannot throw a check exception. I would simplify the code like this.