jQuery in Action, 2nd edition*
The moose likes Beginning Java and the fly likes Compilation error in IF statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compilation error in IF statement" Watch "Compilation error in IF statement" New topic
Author

Compilation error in IF statement

Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
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 ]

Thanks and Regards
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19651
    
  18

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Satya Maheshwari
Ranch Hand

Joined: Jan 01, 2007
Posts: 368
Thanks Rob!!
fred rosenberger
lowercase baba
Bartender

Joined: Oct 02, 2003
Posts: 11153
    
  16

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:



There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Compilation error in IF statement
 
Similar Threads
var arg in method
Array declaration confusion
Final Variable
Question about class and interface
forward referencing doubt.