| Author |
Why there is a compiler error?
|
Supun Lakshan Dissanayake
Ranch Hand
Joined: Oct 26, 2012
Posts: 48
|
|
I know there is no compile error when i use following code
All I need to know what is the difference?
|
 |
fred rosenberger
lowercase baba
Bartender
Joined: Oct 02, 2003
Posts: 9944
|
|
|
The compiler is dumb. In the first example, it is not able to determine that the if statement will always be true.
|
Never ascribe to malice that which can be adequately explained by stupidity.
|
 |
Henry Wong
author
Sheriff
Joined: Sep 28, 2004
Posts: 16686
|
|
Welcome to the ranch ... and next time, can you give full details? You stand a higher chance of getting responses if you describe the error and in as much details as possible. See ...
https://www.coderanch.com/how-to/java/TellTheDetails
Anyway ...
fred rosenberger wrote:The compiler is dumb. In the first example, it is not able to determine that the if statement will always be true.
And in the second case, the compiler is able to determine that the condition will always be true, as the variable x is a compile time constant variable.
Henry
|
Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32644
|
|
Welcome again.
What it means is there is a possibility that there might be another line like x = 0; which would mean y was never assigned to. It is not possible for a compiler to follow the path of execution and test whether a value has changed. In the case of a final variable, however, there is no need to test it; the compiler knows it will not change.
|
 |
Buddhi Vikasitha
Greenhorn
Joined: Nov 01, 2012
Posts: 11
|
|
Campbell Ritchie wrote:Welcome again.
What it means is there is a possibility that there might be another line like x = 0; which would mean y was never assigned to. It is not possible for a compiler to follow the path of execution and test whether a value has changed. In the case of a final variable, however, there is no need to test it; the compiler knows it will not change.
But sheriff, we have already told compiler that x is a final variable, then why thinking about a future x=0 line??
|
 |
Angus Comber
Ranch Hand
Joined: Jul 16, 2011
Posts: 88
|
|
Isn't this an error. You are declaring a final value (but not assigning a value). Then you set a value.
I didn't think this was possible if you declare a value final (unless you assign in constructor).
But interestingly in my Java it doesn't give an error.
|
 |
Jeff Verdegan
Bartender
Joined: Jan 03, 2004
Posts: 5836
|
|
Angus Comber wrote:
Isn't this an error. You are declaring a final value (but not assigning a value). Then you set a value.
I didn't think this was possible if you declare a value final (unless you assign in constructor).
But interestingly in my Java it doesn't give an error.
The rules are different for final static members vs. final non-static members vs. final locals.
static : Must be assigned by the time all static initializers have completed normally.non-static : Must be assigned by the end of all possible paths through constructors have completed normally.local : Must be assigned before it is read.
|
 |
Supun Lakshan Dissanayake
Ranch Hand
Joined: Oct 26, 2012
Posts: 48
|
|
Thanks for the posts guys!
|
 |
Campbell Ritchie
Sheriff
Joined: Oct 13, 2005
Posts: 32644
|
|
You’re welcome
Are there several people in the same class who have been given the same question? We have had quite a few similar posts in the last two weeks.
|
 |
 |
|
|
subject: Why there is a compiler error?
|
|
|