1

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.

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. And if the config file is not exists(IOException will occur), just catch the exception and assign with the default value as well.

But the upper code snippet give a compile time Err:

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

Can I do this?

2 Answers 2

2

Create a method that returns the URL and use to assign it when is declared

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 = "[email protected]";
    iv.numEmployees = 0 ;

    return iv ; 
}
Sign up to request clarification or add additional context in comments.

4 Comments

@armnotstrong I just improved my answer that should work 99% sure ;-)
Do you mean that the improved answer will prevent the case that static block might not be invoked or not invoked properly?
@armnotstrong I just added another sample on another aproach to initialize multiple variables and still follow the same pattern
Thank you for given a more generic solution :D
1

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";
    }
}

4 Comments

you mean the same as my answer ;-) ?
This will be nice, but, in fact, there are more than just one variable that should share the same init logic. It' my fault that I didn't explicit that in the origin post.
Update your post and we'll help you find a solution
@armnotstrong you can wrap all your initial variables into a class and make the method return reference to that class with the initialized values

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.