Good day. I need to initialize the static final variable, I do it in a static initialization block. The point is that the initialization of the variable itself can throw an exception, I wrapped the initialization in try {} catch, but then the compiler tells me that the variable may not be initialized, it is correct, but then how can I initialize this variable? Here is how the code example:

public class Example { public static final Connection connection; static { try { connection = DriverManager.getConnection("jdbc:sqlite:" + PATH); //Вот эта строка может выкинуть исключительную ситуацию } catch (Exception ignored) {} } } 
  • connection = null; - Igor

1 answer 1

 public class Example { public static final Connection connection; static { Connection c = null; try { c = DriverManager.getConnection("jdbc:sqlite:" + PATH); } catch (Exception exc) {} finally { connection = c; } } } 
  • Thank you very much - ProstoCoder