简体   繁体   English

在静态try-catch中分配静态final变量

[英]assign static final variable in a static try-catch

I want to read a property for config file and assign it to a static final variable and if in case the config file is omit/or not exists, using default value hard-coded. 我想读取配置文件的属性并将其分配给static final变量,如果配置文件是省略/不存在,则使用默认值硬编码。

public static final String NOTIFY_ALI;
static  {
    try {
        PropertyReader notifyConf = new PropertyReader("notify.conf");
        NOTIFY_ALI = notifyConf.getProperty("notify_ali","http://notify.foo.com/notify");

    } catch (IOException e) {
        e.printStackTrace();
        NOTIFY_ALI = "http://notify.foo.com/notify";
    }
}

NOTIFY_ALI should be assigned by the configure file notify.conf with the key notify_ali or if it's not explicit in the file, take http://notify.foo.com/notify as a default value. NOTIFY_ALI应该由配置文件notify.conf分配,密钥为notify_ali或者如果文件中没有显式,请将http://notify.foo.com/notify作为默认值。 And if the config file is not exists( IOException will occur), just catch the exception and assign with the default value as well. 如果配置文件不存在(将发生IOException ),只需捕获异常并分配默认值。

But the upper code snippet give a compile time Err: 但是上面的代码片段给出了编译时间Err:

Error:(18, 13) java: variable NOTIFY_ALI might already have been assigned

Can I do this? 我可以这样做吗?

Create a method that returns the URL and use to assign it when is declared 创建一个返回URL的方法,并在声明时使用它来分配它

public static final String NOTIFY_ALI = getURL() ;

private static String getURL()
{ 
    String aux ;

    try {
        PropertyReader notifyConf = new PropertyReader("notify.conf");
        aux = notifyConf.getProperty("notify_ali","http://notify.foo.com/notify");

    } catch (IOException e) {
        e.printStackTrace();
        aux = "http://notify.foo.com/notify";
    }
    return aux ; 
}

If you need to initialize more than one variable you could do it like this 如果你需要初始化多个变量,你可以像这样做

public static final InitVariables IV = initVariables() ;

public class InitVariables {
    String NOTIFY_ALI ;
    String CONTACT_EMAIL ;
    int numEmployees ; 
}

private static InitVariables initVariables()
{ 
    InitVariables iv ;

    iv = new InitVariables() ;

    try {
        PropertyReader notifyConf = new PropertyReader("notify.conf");
        aux = notifyConf.getProperty("notify_ali","http://notify.foo.com/notify");

    } catch (IOException e) {
        e.printStackTrace();
        aux = "http://notify.foo.com/notify";
    }

    iv.NOTIFY_ALI = aux ;

    iv.CONTACT_EMAIL = "you@somedomain.com";
    iv.numEmployees = 0 ;

    return iv ; 
}

Why don't you remove the static initializer block and create a static method whose result is assigned to your static final variable? 为什么不删除静态初始化程序块并创建一个静态方法,其结果分配给静态最终变量?

public static final String NOTIFY_ALI = init();

private static String init() {
    try {
        PropertyReader notifyConf = new PropertyReader("notify.conf");
        return  notifyConf.getProperty("notify_ali","http://notify.foo.com/notify");
    } catch (IOException e) {
        e.printStackTrace();
        return "http://notify.foo.com/notify";
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM