I have a class like:
public class SomeClassImpl implements SomeClass {
private static final SomeLib someLib = new SomeLib();
}
I can\'t
You can use static initializer:
public class SomeClassImpl implements SomeClass {
private static final SomeLib someLib;
static {
SomeLib tmp = null;
try {
tmp = new SomeLib();
} catch (UnknownHostException uhe) {
// Handle exception.
}
someLib = tmp;
}
}
Note that we need to use a temporary variable to avoid "variable someLib might not have been initialized" error and to cope with the fact that we can only assign someLib once due to it being final.
However, the need to add complex initialization logic and exception handling to static initializer is often a sign of a more fundamental design issue. You wrote in the comments section that this is a database connection pool class. Instead of using static final consider making it an instance variable. You can then do the initialization in a constructor or better yet in a static factory method.