1

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.

drarkayl
  • 1,017
  • 7
  • 17

5 Answers5

2
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 :).

Suresh Atta
  • 120,458
  • 37
  • 198
  • 307
  • yes but im not really re-assigning it. it is assigned only once in the course of the program. – drarkayl Jun 24 '13 at 07:27
  • x=2; is a assignment statement right ?? The possibility here is add a else condition and assign there. – Suresh Atta Jun 24 '13 at 07:31
  • but only one of those statements are reached when the execution happens – drarkayl Jun 24 '13 at 07:33
  • 2
    @drarkayl Corrent, **"...when the execution happens"**, but this is a compile time error, and since your `System.exit(0);` call doesn't have a `return` following, the compiler assumes that the assignment after the if will be executed as well. – LuigiEdlCarno Jun 24 '13 at 07:36
  • @LuigiEdlCarno exactly the answer i was looking for. Thank you. – drarkayl Jun 24 '13 at 07:38
2

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;
    }
Community
  • 1
  • 1
sanbhat
  • 17,522
  • 6
  • 48
  • 64
  • Thank you for the answer, i got your point but if you see the flow of execution im not assigning it twice.It is assigned only once. – drarkayl Jun 24 '13 at 07:32
  • you say `x=1` and then `x=2` so `x` is getting assigned twice – sanbhat Jun 24 '13 at 07:34
  • 1
    @drarkayl System.exit is not considered as code termination...its not a return.. so as per compiler, code after System.exit() may execute – sanbhat Jun 24 '13 at 07:39
1

Once you have declared some variable as final then you cannot asign it a value

EduardoSaverin
  • 545
  • 4
  • 19
1

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.

Szidor
  • 210
  • 1
  • 8
0

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...

Rupesh
  • 2,627
  • 1
  • 28
  • 42