简体   繁体   中英

Final variable assignment error

I dont know if this is a stupid question or not but please try to answer it.

public static void main(String[] args){
    int i=0;
    final int x;
    if(i==0){
        x=1;
        System.exit(0); 
    }
    x=2;
}

I have a final variable x.

Now to assign a value to xi have an if statement that assigns it and the exits the program.

Now the last statement is never reached and thus this program should compile logically.

x will have value 1 or 2 depending on the if statement. If the 'if' is true the last statement is not reached and if it is false the 'x=1' statement is never reached.

So why is this giving me a compile error of 'local' variable has been initialized?

EDIT:

yes i do obviously know that a final statement can be assigned only once.

what my doubt was that only one of those statements will be reached during execution so looking at it that way the program would have only one assignment statement.

Final is Final 

once you declared and assigned,You cannot assign it again.

And the final assignment is a compile time check.Even you are exiting function before still it will do its duty :).

一旦将某个变量声明为final,就无法将其赋值

The compiler doesn't know anything else of System.exit that it's a function. It assumes, execution will continue.

Add return after System.exit and it will compile.

x will have value 1 or 2 depending on the if statement. If the 'if' is true the last statement is not reached and if it is false the 'x=1' statement is never reached.

This is not true, since you DONT have if followed by else .

Also, Since System.exit(0) is merely a function call and not a different code path, the Java compiler assumes the code after it, to be very much reachable . See this thread for more clarity

As far as the final variable is concerned, it cannot be assigned twice .

The below code would work without error, since i==0 can be either true or false , and x gets assigned only once

    int i=0;
    final int x;
    if(i==0){
        x=1;
        System.exit(0); 
    }
    else {
        x=2;
    }

There is a concept of "Definite Assignment" is java. That goes like this.

A Java compiler must carry out a specific conservative flow analysis to make sure that, for every access of a local variable or blank final field f, f is definitely assigned before the access; otherwise a compile-time error must occur.

The idea behind definite assignment is that an assignment to the local variable or blank final field must occur on every possible execution path to the access. The analysis takes into account the structure of statements and expressions; it also provides a special treatment of the expression operators !, &&, ||, and ? :, and of boolean-valued constant expressions.

Now as I have mentioned that flow analysis checks for Definite Assignment and it happens in if clause in your case and outside you again try to change the value of x and this will not be allowed...

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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