Things to know when you write your first program
One class per file
- Although it's technically possible to have multiple classes in a single file, for now please have one class per file. You may have problems otherwise.
Java is case-sensitive
DrawSquare
is not the same as Drawsquare
. Upper and lowercase are different identifiers to Java.
File and class name must match
- If your class name doesn't match the file name exactly (but without the
.java
extension), your code won't compile. For example, DrawSquare.java
should contain class DrawSquare
.
Spaces are not allowed in class names and variable names.
- Additionally, you cannot begin a class or variable name with a numerical value. You can have numbers after the first letter, however. Some special characters such as underscores are allowed but not conventionally used. This isn't C programming.
Braces matter for compiling
- Make sure every opening brace has a matching closing brace
{ }
Indenting matters for readability
- The contents inside every pair of braces
{ }
should be indented one tab.
"gpdraw.*
" cannot be found
- If you get this error then you didn't correctly follow steps 4 and 5 on the Java Setups Lesson. It's also possible that you downloaded gpdraw.jar incorrectly. When downloading
gpdraw.jar
some web browsers try to change the filename to gpdraw.zip
. Don't allow that. Change it back to gpdraw.jar
.
Selective debugging
- If you have an error but don't know what line it's on (BlueJ doesn't always tell you the correct line number) then a very good debugging technique is to comment out all of the individual instruction lines (any line ending in a semicolon) and look at the overall structure. If you still have compiling problems then you're missing a brace somewhere. Otherwise, you can start uncommenting the instruction lines one by one until you find the problem.