Java Reference
In-Depth Information
It's possible for you to introduce an error before you even start writing your pro-
gram, if you choose the wrong name for its file.
Common Programming Error
File Name Does Not Match Class Name
As mentioned earlier, Java requires that a program's class name and file name
match. For example, a program that begins with public class Hello must be
stored in a file called Hello.java .
If you use the wrong file name (for example, saving it as WrongFileName.java ),
you'll get an error message like this:
WrongFileName.java:1: class Hello is public, should be
declared in a file named Hello.java
public class Hello {
1 error
The file name is just the first hurdle. A number of other errors may exist in your
Java program. One of the most common syntax errors is to misspell a word. You may
have punctuation errors, such as missing semicolons. It's also easy to forget an entire
word, such as a required keyword.
The error messages the compiler gives may or may not be helpful. If you don't
understand the content of the error message, look for the caret marker (
) below the
line, which points at the position in the line where the compiler became confused.
This can help you pinpoint the place where a required keyword might be missing.
Common Programming Error
Misspelled Words
Java (like most programming languages) is very picky about spelling. You need
to spell each word correctly, including proper capitalization. Suppose, for exam-
ple, that you were to replace the println statement in the “hello world” pro-
gram with the following:
System.out.pruntln("Hello, world!");
When you try to compile this program, it will generate an error message similar
to the following:
Hello.java:3: cannot find symbol
symbol : method pruntln(java.lang.String)
Continued on next page
 
Search WWH ::




Custom Search