Two Laptop Bag
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
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Compilation error in IF statement" Watch "Compilation error in IF statement" New topic

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

Joined: Oct 27, 2005
Posts: 20080

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.

How To Ask Questions How To Answer Questions
Satya Maheshwari
Ranch Hand

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

Joined: Oct 02, 2003
Posts: 11860

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
I agree. Here's the link:
subject: Compilation error in IF statement
jQuery in Action, 3rd edition