Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Compilation error in IF statement

 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am getting compilation error in one of the 2 IF statements which I consider equivalent.



What am I missing here
[ October 24, 2008: Message edited by: Satya Maheshwari ]
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An if-statement, and also all others like for, while etc, require a statement. Now "int b = 10;" is not a statement, it's a declaration. Using braces will work: it wraps all code inside the braces into one larger compound statement. And compound statements can have declarations.
 
Satya Maheshwari
Ranch Hand
Posts: 368
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Rob!!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12087
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
remember, though, that if you do this, the variable 'b' will not be usable outside the block:



I don't know exactly how you will be using it, but you might want to consider this instead:

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic