Reputation: 1017
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 x i 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.
Upvotes: 1
Views: 1194
Reputation: 2667
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...
Upvotes: 0
Reputation: 17622
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;
}
Upvotes: 2
Reputation: 210
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.
Upvotes: 1
Reputation: 545
Once you have declared some variable as final then you cannot asign it a value
Upvotes: 1
Reputation: 121998
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 :).
Upvotes: 2