• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Why there is a compiler error?

 
Supun Lakshan Dissanayake
Ranch Hand
Posts: 135
Android Java PHP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 12084
29
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler is dumb. In the first example, it is not able to determine that the if statement will always be true.
 
Henry Wong
author
Marshal
Pie
Posts: 20882
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
Campbell Ritchie
Sheriff
Posts: 48382
56
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
Java Netbeans IDE Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 90
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 135
    Android Java PHP
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks for the posts guys!
     
    Campbell Ritchie
    Sheriff
    Posts: 48382
    56
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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.
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic