This week's giveaway is in the EJB and other Java EE Technologies forum.
We're giving away four copies of EJB 3 in Action and have Debu Panda, Reza Rahman, Ryan Cuprak, and Michael Remijan on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes statement unreachable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "statement unreachable" Watch "statement unreachable" New topic
Author

statement unreachable

Niral Trivedi
Ranch Hand

Joined: Nov 26, 2001
Posts: 46
All,
Shouldn't following code give 'Statement Unreachable' compilation error for 'else' part??? Because variable 'boo' is always true!!! Instead it compiles and prints 'if' successfully!!!

Please help..
Thank you..
Niral
Meg Adal
Ranch Hand

Joined: Mar 12, 2002
Posts: 70
All,
Shouldn't following code give 'Statement Unreachable' compilation error for 'else' part??? Because variable 'boo' is always true!!! Instead it compiles and prints 'if' successfully!!!
boo is variable means its value can changed any time anywere in the program
if boo were final boolean boo =true; you would get unreachable statement error as boo is final so its value can't be changed
[ September 06, 2002: Message edited by: Megan Adal ]
janus place
Greenhorn

Joined: Aug 14, 2002
Posts: 8
Even if you have final boolean boo = true;
The else part will compile ok and the program will print out "if" successfully.
Instead of an if condition check, if you have a while loop, this will give compile error :
class Buggy {
public static void main ( String args [ ] ) {
final boolean boo = true ;
if ( boo ) {
System.out.println ( " if " ) ;
} else;

while (!boo){
System.out.println ( " while " ) ;
}
}
};
Compile error on printing while !!
HTH,
Janus
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
The if construct has been spared from checking for unreachable statement. The reason being that some time developer wants to do conditional compilation. All other conditional constructs will be checked for unreachable statement and compiler will let you know...
Niral Trivedi
Ranch Hand

Joined: Nov 26, 2001
Posts: 46
Thank you all..
Niral
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: statement unreachable
 
Similar Threads
Clarify...while....
statement not reached error
Why no compilation error?
Assert Doubt
Two different subclass objects within an arraylist.