Can't define a private static final variable because it throws an exception

后端 未结 5 753
轻奢々
轻奢々 2020-12-20 11:38

I have a class like:

public class SomeClassImpl implements SomeClass {
   private static final SomeLib someLib = new SomeLib();
}

I can\'t

相关标签:
5条回答
  • 2020-12-20 11:45

    Even just a little bit more elegant than the solution from Adam Zalcman:

    public class SomeClassImpl implements SomeClass {
       private static final SomeLib someLib = initSomeLib();
    
       private static SomeLib initSomeLib() {
         SomeLib someLib = null;
         try {
           someLib = new SomeLib();
         } catch (UnknownHostException uhe) {
           // Handle exception.
         }
         return someLib;
       }
    
    }
    
    0 讨论(0)
  • 2020-12-20 11:50

    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.

    0 讨论(0)
  • 2020-12-20 12:01

    You could use a static initializer:

    private static final SomeLib SOME_LIB; // respect naming conventions
    
    static {
        try {
            SOME_LIB = new SomeLib();
        }
        catch (UnknownHostException e) {
            throw new RuntimeException("Class initialization failed due to UnknownHostException", e);
        }
    }
    

    Note that your class won't be able to initialize if you do it so. Maybe you should try to initialize the lib lazily, when needed. Such class initialization exceptions are hard to diagnose, because they're transformed into ClassNotFoundException or NoClassDefFoundError (I don't remember which one)

    0 讨论(0)
  • 2020-12-20 12:04

    This sample code will gives you an idea on initializing multiple varibales with out using static method:

    public class SomeClass implements SomeOtherClass{
    
    private String string1= getValues("/var/log/log1.txt");
    private String component = getValues("/var/log/log2.txt");
    
    private String getValues(String file) {
            try {
              return  new Scanner(new File(file)).next();
            }catch(FileNotFoundException ioe){
                System.out.println("File not found :: " +ioe);
            }
            return null;
        }
    
    0 讨论(0)
  • 2020-12-20 12:09

    If initialising a variable is likely to fail, then it is likely a poor candidate to be static.

    Statics are good for [effective] immutables, (with care) caches of immutables and little else.

    0 讨论(0)
提交回复
热议问题