Meaningless Drivel is fun!*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes non rechable statment? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "non rechable statment?" Watch "non rechable statment?" New topic
Author

non rechable statment?

Rishi Wright
Ranch Hand

Joined: Jun 25, 2003
Posts: 46
Hi 'All
here is the code
class Test {
void notReachable() {
int i;
if(false)
i=6;
// System.out.println(i);


}
public static void main(String[] args) {
Test t=new Test();
t.notReachable();
}
}
In the above code false is a boolean constant always equals to false that means the following statement will be never reached ...then why the compiler is not giving any not reachable error?
thnx
rishi
Ron Newman
Ranch Hand

Joined: Jun 06, 2002
Posts: 1056
Because the Java language spec has a special rule allowing the "if (false)" construct and saying that it doesn't make any code unreachable. The purpose is to easily allow turning debugging booleans on and off.


Ron Newman - SCJP 1.2 (100%, 7 August 2002)
Rishi Wright
Ranch Hand

Joined: Jun 25, 2003
Posts: 46
then how come below code is giving an error???
class Test {
public static void main(String[] args) {
Test t=new Test();
System.out.println(t.notReachable());
}
String notReachable() {
int i;
if(false)
return "String";


}
}
thnx
rishi
venu gopal
Ranch Hand

Joined: Nov 21, 2000
Posts: 40
Hi Rishi,
The method is expecting return statement. Either you must provide else part or include semicolon after if statement, to make compiler happy..

class Test {
public static void main(String[] args) {
Test t=new Test();
System.out.println(t.notReachable());
}
String notReachable() {
int i;
if(false)
return "String";

/*else
return "Str";
*/

}
}
String notReachable() {
int i;
if(false)
/*
; */

return "String";
}
Thanks,
Venu Gopal.
[ August 13, 2003: Message edited by: venu gopal ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: non rechable statment?
 
Similar Threads
junit test for set Method with boolean input and void return
Regular Expressions: A String should not contain the word "TEST"
Enthuware question doubt
Byte and String
inner class