wood burning stoves 2.0*
The moose likes Java in General and the fly likes Compile errors when assigning Final variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Compile errors when assigning Final variable" Watch "Compile errors when assigning Final variable" New topic
Author

Compile errors when assigning Final variable

Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
Moderators: sorry for cross posting (this and IDE), but I was not sure if this was an Eclipse problem, or a language misunderstanding on my part.

My understanding of Final fields is that they cannot be changed once assigned. Therefore, this should work as I understand it:



However Eclipse and javac1.5 from the command line both bark at me stating the variables may have already been assigned, and/or may not have been initialized.

Any ideas?


Tom Blough<br /> <blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>Cum catapultae proscriptae erunt tum soli proscripti catapultas habebunt.<hr></blockquote>
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Because the assignments are in a for loop, they may execute zero times, or multiple times. This is against the rules, as you seem to be aware.

You could introduce some temporary variables, and move the assignments outside of the loop, so they are made once and only once:



[Jess in Action][AskingGoodQuestions]
Tom Blough
Ranch Hand

Joined: Jul 31, 2003
Posts: 263
DUH!

I completely overlooked the fact that the loop executes the ifs multiple times!

[running and hiding in embarrassment...]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Originally posted by Tom Blough:

[running and hiding in embarrassment...]


No, please, come back...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Compile errors when assigning Final variable