wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes What does this compile error mean? 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 "What does this compile error mean?" Watch "What does this compile error mean?" New topic
Author

What does this compile error mean?

Bin Wang
Ranch Hand

Joined: May 01, 2001
Posts: 82
class Test {
int y=32;
int a;

Test() {
this(6);
}
Test(int x) {
pause:
for ( int i=0; i < 2 ; i++) { <br /> if ( x > y >> 3 ) {
continue pause;
a = y*6; //line 14, compile error: Statement not reached
}
else {
a = y*9;
}
a++;
System.out.println(a);
} //end of for loop
} //end of Test(int x) constructor

public static void main(String [] args) {
Test t = new Test();
}
}
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
Bin, in this case the compiler message is actually pretty accurate.


line 1 will always send the program back to the for loop, thereby preventing the following line from ever being executed. The compiler checks to see if utterly unreachable statements exist in the code and flags them (since they can never be reached, why do they exist in the code at all?).
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
Bin,
It looks to me like the compiler is right. In your loop:
pause:
for ( int i=0; i < 2 ; i++) { <br /> if ( x > y >> 3 ) {
continue pause;
a = y*6; //line 14, compile error: Statement not reached
} else {
a = y*9;
}
if y right shifted 3(was this a typo?) is less than x then it continues the labeled loop. If it's not then it multiplies y * 9 and assigns the result to a. so regardless if the test is true or false it can not reach the line after the continue statement. The other thing is that through out the lopp none of the value in the if statement change, regardless of howmany times you go through the loop and increment i neither x nor y change value so the test result will always be the same.
I can't be sure exactly what you were going for here but I assume you want to put the a = y*6; as the line before your continue statement. And maybe the y is supposed to be an i?
hope that helps
Dave


Dave
Bin Wang
Ranch Hand

Joined: May 01, 2001
Posts: 82
Hi,
First of all,there is no typo in the program.
Test(int x) {
pause:
for ( int i=0; i < 2 ; i++) { <br /> if ( x > y >> 3 ) {
continue pause;
a = y*6;
}
else {
a = y*9;
}
a++;
System.out.println(a);
} //end of for loop
} //end of Test(int x) constructor

As X > y >> 3 always return ture, the else part of the if block will never get a chance to execute. But if I delete a=y*6; the program will compile and run, why the compile doesn't check it?
Is that true that the compile just check the unreachable statements in the if block?
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
Bin,
This is a bit of a subtle distinction, but the compiler plays dumb in this instance. It's not going to bother to do all the math and run a "simulation" to see whether the else block will be reached.
You and I (and Dave, as his post described) can figure out that the else won't be executed. You kind of have to hit the compiler upside the head with a shovel to make it realize that a piece of code cannot be reached. The "continue pause" statement placed immediately before another statement in your original code is egregious enough to make the compiler take note. If there is no statement following the "continue pause" within the if block then the compiler will not complain
The compiler will check the else block as well, and if you stick "continue pause" in front of the assignment statement in the else block you will get the Unreachable Statement error.

[This message has been edited by Scott Appleton (edited May 29, 2001).]
[This message has been edited by Scott Appleton (edited May 29, 2001).]
Bin Wang
Ranch Hand

Joined: May 01, 2001
Posts: 82
I think I got it. The word "utterly" is very important. The compiler will flag the utterly unreachable statement(s).
Thanks Scott and Dave.
Bin Wang
Ranch Hand

Joined: May 01, 2001
Posts: 82
In fact, x > y >> 3 will not always return true. it depends on what the value of x and y. I'm wrong in my previous previous message.
One thing I'm sure now is: the compiler will flag the UTTERLY unreachable statement(s).
Scott Appleton
Ranch Hand

Joined: May 07, 2001
Posts: 195
This being the JavaRanch, perhaps I should have said "udderly" unreachable statements"
Dave Vick
Ranch Hand

Joined: May 10, 2001
Posts: 3244
uggghhh
Scott!! bad!!
farooqmali
Greenhorn

Joined: Apr 01, 2001
Posts: 8
class Test {
int y=32;
int a;
Test() {
this(6);
}
Test(int x) {
int a; //This I think, i guess every thing is right. but i dont have explanation for that.
pause:
for ( int i=0; i < 2 ; i++) { <br /> if ( x > y >> 3 ) {
continue pause;
a = y*6; //line 14, compile error: Statement not reached
}
else {
a = y*9;
}
a++;
System.out.println(a);
} //end of for loop
} //end of Test(int x) constructor
public static void main(String [] args) {
Test t = new Test();
}
}

Muhammad Ali Farooq
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: What does this compile error mean?
 
Similar Threads
about supper() and this(..)
about this(..)
Why do i keep getting the "type expected" error
Compile Error: Statement never reached?
abstract class instantiation