Based on my understanding of the Java language, static variables can be initialized in static initialization block
.
However, when I try to implement this in practice (static
variables that are final
too), I get the error shown in the screenshot below:
Assuming no where upstream is in a position to catch either an ExceptionInInitializationError or a general Exception then the program should not ever try to use myVar. If however those are caught and the program doesn't end, then you need to code to watch for and handle myVar being null (or be happy with
NullPointerExceptions
coming out all over).I'm not sure there is a good way to handle this.
Yes of course:
static final
variables can be initialized in a static block but.... you have implicit GOTOs in that example (try/catch
is essentially a 'GOTO catch if something bad happens').If an exception is thrown your
final
variables will not be initialized.Note that the use of static constructs goes against Object-Oriented dogma. It may complicate your testing and make debugging more difficult.
Can you put the declaration in the finally block?
You can do this but you need to exit the static block by throwing an exception - you can rethrow the exception that was caught or a new one. Generally this exception must be a
RuntimeException
. You really should not catch a genericException
but more specific exception(s) that might be thrown from within yourtry
block. Finally, if a static initializer throws an exception then it will render the class unusable during that specific run because the JVM will only attempt to initialize your class once. Subsequent attempts to use this class will result in another exception, such asNoClassDefFoundError
.So, to work, your initializer should read something like this:
Assuming that
InitializationFailedException
is a customRuntimeException
, but you could use an existing one.