File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compile errors when assigning Final variable

 
Tom Blough
Ranch Hand
Posts: 263
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:

 
Tom Blough
Ranch Hand
Posts: 263
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Tom Blough:

[running and hiding in embarrassment...]


No, please, come back...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic