Reputation: 21
I have a class where I am populating variable values as below
public class JSONDBWriter{
// JDBC driver name, database URL, Database credentials
private static final String JDBC_DRIVER = ReadPropertiesFile("JDBC_DRIVER");
private static final String DB_URL = ReadPropertiesFile("DB_URL");
private static final String USER = ReadPropertiesFile("USER");
private static final String PASS = ReadPropertiesFile("PASS");
public static void main(String[] args) {
However, Java compiler is giving error "Unhandled Exception type IOException" My ReadPropertiesFile throws IOException.
Upvotes: 0
Views: 793
Reputation: 16389
Use static initializer.
public class JSONDBWriter {
public static String driver;
static {
try{
driver = //...
} catch (IOException e){
//
}
}
//other methods
}
If you want your variables to be final, try something like below:
public class Test {
public static final String test = getDataNoException();
private static String getData() throws IOException {
return "hello";
}
private static String getDataNoException() {
try {
return getData();
} catch (IOException e) {
e.printStackTrace();
return "no data";
}
}
//other methods
}
Upvotes: 3