Oyst
Oyst

Reputation: 1

Why is this NOT unreachable in the if-statement?

In testOne:

In testTwo:

Is all this because the compiler somehow thinks my variable and boolean can change even if they are final("aNumber") and hardcoded("true"). And when there is no variable or boolean (the first "else" in testTwo) nothing can change, and since it mops up all possibilities by just saying "else" it knows it cant reach anything further down?

public int testOne() {
        final int aNumber = 5;
        if (aNumber < 10) {
            return 0;
        } else if (true) {
            return 2;
        } else {
            return 3;
        }
    }

public int testTwo() {
        final int aNumber = 5;
        if (aNumber < 10) {
            return 0;
        } else {
            return 1;
        }
        if (true) {
            return 3;
        } else {
            return 4;
        }
    }

Upvotes: 0

Views: 33

Answers (0)

Related Questions