Java Reference
In-Depth Information
Continued from previous page
4 System.out.println("would smell as sweet");
5 }
6 }
In this case, the compiler produces output similar to the following:
MissingSemicolon.java:4: ';' expected
System.out.println("would smell as sweet");
1 error
The odd thing about the compiler's output is that it has listed line 4 as the
cause of the problem, not line 3, where the semicolon was actually forgotten.
This is because the compiler is looking forward for a semicolon and isn't upset
until it finds something that isn't a semicolon, which it does when it reaches line
4. Unfortunately, as this case demonstrates, compiler error messages don't
always direct you to the correct line to be fixed.
Common Programming Error
Forgetting a Required Keyword
Another common syntax error is to forget a required keyword when you are typing
your program, such as static or class . Double-check your programs against the
examples in the textbook to make sure you haven't omitted an important keyword.
The compiler will give different error messages depending on which keyword is
missing, but the messages can be hard to understand. For example, you might
write a program called Bug4 and forget the keyword class when writing its class
header. In this case, the compiler will provide the following error message:
Bug4.java:1: class, interface, or enum expected
public Bug4 {
1 error
However, if you forget the keyword void when declaring the main method, the
compiler generates a different error message:
Bug5.java:2: invalid method declaration; return type required
public static main(String[] args) {
1 error
 
Search WWH ::




Custom Search