vendredi 3 mai 2019

Java: how to declare a final String inside a constructor with an if-statement

I used to declare a final String inside a constructor. Now I want to insert an if-statement in order to declare it differently if needed.

I used to do:

public Config(){
    final String path = "<path>";
    doSomething(path);
}

Now I'm trying

public Config(String mode){
    if (mode = "1") {
        final String path = "<path1>";
    } else {
        final String path = "<path2>";
    }
    doSomething(path);
}

Unfortunately path cannot be found now (cannot find symbol error) and I'm really lost with my research understanding this. The following works though, I just cannot explain..I must have a deep miss conception about something here.

public Config(String mode){
    final String path;
    if (mode = "1") {
        path = "<path1>";
    } else {
        path = "<path2>";
    }
    doSomething(path);
}

Can you explain me what is going on here, what should I read about to get this.

Thank you in advance

Aucun commentaire:

Enregistrer un commentaire