The following list describes some errors that are easy to make in writing Java ...
The Java language is a strict language. A variable or method CAN NOT be used before it is declared and a common mistake is to misspell the variable/method name when using it. Here are some general rules for naming variables in java:
The Java language is Case Sensitive! For example, Java will not recognize the word string as a valid type in the language as you should have written String.
Java will generate an error message of the form ...
Line nn: Class xxxx not found in type declaration.
... where xxxx is the name of the class which has not been given the correct capitalization.
The Java language is Case Sensitive! It is a common mistake to miss the fact that variables are case sensitive. For example, you may have declared the variable Name as an String and then later on in your program you try to refer to the variable name.
This gives rise to error messages of the form ...
Line nn: Undefined variable: xxxx
... where xxxx is the name of the variable which has been mistyped.
The Java language is Case Sensitive! So that the method System.out.print() is different from the method system.out.print() and the method main() is different than the method Main().
The Java compiler will issue an error message such as ...
Line nn: class or interface declaration expected
... when it does not recognize a variable, method or Java Object because it is misspelled (including its case)
This is a common programming error in many programming languages and can be fixed by adding the closing bracket. All blocks of code must be started with a { bracket and ended/closed with a }bracket. This type of error can be reduced by using a good code indentation system.
This is a common programming error in many programming languages and can be fixed by adding the closing bracket. Methods must use the ( and ) to surround their parameter list.
This is a common programming error for beginning programmers. It is important to know when to use the different types of bracket.
{ }
Used for blocks of code - like loops, if statements, classes and method
( )
Used in method headers to surround the parameter list
Use in Math equations
[ ]
Used in arrays to denote the array index position
This is a common programming error for beginning programmers. Each block of code surround by the { and } brackets must be properly structured so that two blocks of code are not straggled but rather one block of code must be contained inside of another block of code.
This type of error can be reduced by using a good code indentation system.
When you use a method which has no arguments you should place brackets after the name of the method.
For example, if you have declared a method PrintBlankScreen
with no arguments then you should code this as:
PrintBlankScreen( )
rather than:
PrintBlankScreen
The compiler will usually indicate an error message of the form:
Line nn: Invalid expression statement
Class methods have the form:
ClassName.MethodName(Argument(s))
A common error is to forget the class name. If you do, then you will get an error message of the form:
Line nn: '}' expected
When you define classes you should prefix each argument with the name of a scalar type or the name of an existing class. For example:
public void tryIt(int a, int b, URL c)
A common error that programmers from other languages make is to forget to prefix every argument with its type. For example, an erroneous version of the definition above would be:
public void tryIt(int a, b URL c)
This type of error will give rise to error messages of the form:
Line nn: Identifier expected
When a method returns no result, but just carries out some action, you need to use the keyword void in front of the name of the method. If you do not use this keyword, then it will give rise to error messages of the form:
Line nn: Invalid method declaration; return type required
When a method returns a value, then the body of the method should include at least one return statement which returns the right type of value.
Failing to include a return statemnet will generate an error message of the form ...
Line nn: Return required at end of xxxx
... where xxxx is the method which does not contain the return.
Static methods are associated with messages sent to classes rather than objects. A common error is to send static method messages to objects. For example, in order to calculate the absolute value of an int value and place it into the int variable you should write:
int result = Math.abs(value);
rather than:
int result = value.abs();
This gives rise to a variety of syntax errors. The most common one is of the form:
Line nn: Method yyyy not found in class xxxx.
where yyyy is the name of the method and xxxx is the name of the class within which it is called.
This one of the most common errors that inexperienced Java programmers make. If you forget to put the required import statement at the beginning of a program, then the compiler will respond with a message such as ...
Line nn: Class xxxx not found in type declaration
Don't forget, though, that java.lang is imported automatically and, hence, does not need an import statement.