*
The moose likes Beginning Java and the fly likes Why there is a compiler error? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Why there is a compiler error?" Watch "Why there is a compiler error?" New topic
Author

Why there is a compiler error?

Supun Lakshan Dissanayake
Ranch Hand

Joined: Oct 26, 2012
Posts: 132


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: 11250
    
  16

The compiler is dumb. In the first example, it is not able to determine that the if statement will always be true.


There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18754
    
  40


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: 38461
    
  23
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: 17

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: 90


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: 6109
    
    6

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: 132

    Thanks for the posts guys!
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 38461
        
      23
    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.
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: Why there is a compiler error?