• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Why there is a compiler error?

 
Ranch Hand
Posts: 143
Android Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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://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
 
Marshal
Posts: 79179
377
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 17
Netbeans IDE Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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??
 
Ranch Hand
Posts: 90
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


  •  
    Lakshan Dissanayake
    Ranch Hand
    Posts: 143
    Android Java Linux
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • Quote
    • Report post to moderator
    Thanks for the posts guys!
     
    Campbell Ritchie
    Marshal
    Posts: 79179
    377
    • Mark post as helpful
    • send pies
      Number of slices to send:
      Optional 'thank-you' note:
    • 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.
     
    Consider Paul's rocket mass heater.
    reply
      Bookmark Topic Watch Topic
    • New Topic