MY mENU


Friday, 21 September 2012

Syntax Error in Java


Frequently, a single small mistake will cause the compiler to issue a cascade of messages. For example, in the following code, we forgot to write the closing brace of the method f:
class MyClass {
void f() {
int n = 10;
// Error, closing brace is missing
void g() {
int m = 20;
}
}
An attempt to compile the program results in three error messages:

MyClass.java:5: illegal start of expression
void g() {
^
MyClass.java:8: ’;’ expected
}
^
MyClass.java:9: ’}’ expected
^
Do not invest any effort in trying to fix multiple error messages! Concentrate on fixing the first error and then recompile. ECLIPSE is much better at diagnosing this error and produces only one message: Syntax error, insert "}" to complete MethodBody.